Advertisement
urksiful

Main Actitivty

May 17th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package pro.izcali.u4drawing;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.graphics.Bitmap;
  6. import android.graphics.Color;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.os.Environment;
  10. import android.support.design.widget.FloatingActionButton;
  11. import android.support.design.widget.Snackbar;
  12. import android.support.v4.app.ActivityCompat;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.support.v7.widget.Toolbar;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. import android.widget.RelativeLayout;
  20. import android.widget.Toast;
  21.  
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.FileOutputStream;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.     RelativeLayout parent;
  28.     CanvasDrawing drawView;
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {    
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.         if (getPermissions()) {
  34.             Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  35.             setSupportActionBar(toolbar);
  36.  
  37.             parent = (RelativeLayout) findViewById(R.id.canvasDrawing);
  38.             drawView = new CanvasDrawing(this);
  39.  
  40.             parent.addView(drawView);
  41.  
  42.  
  43.             FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  44.             fab.setOnClickListener(new View.OnClickListener() {
  45.                 @Override
  46.                 public void onClick(View view) {
  47.                     saveDraw();
  48.                     Snackbar.make(view, "Guardado exitosamente", Snackbar.LENGTH_LONG)
  49.                             .setAction("UNDO", null).show();
  50.                 }
  51.             });
  52.  
  53.             FloatingActionButton fd = (FloatingActionButton) findViewById(R.id.fd);
  54.             fd.setOnClickListener(new View.OnClickListener() {
  55.                 @Override
  56.                 public void onClick(View view) {
  57.                     drawView = new CanvasDrawing(view.getContext());
  58.  
  59.                     parent.addView(drawView);
  60.                 }
  61.             });
  62.         }else{
  63.             finish();
  64.         }
  65.     }
  66.     private boolean getPermissions() {
  67.         Boolean granted = false;
  68.  
  69.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  70.             if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
  71.  
  72.  
  73.                 granted = true;
  74.             }else{
  75.  
  76.                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  77.  
  78.                 return false;
  79.             }
  80.         } else {
  81.             granted = true;
  82.         }
  83.  
  84.         return granted;
  85.     }
  86.     @Override
  87.     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  88.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  89.         if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
  90.           getPermissions();
  91.         }
  92.     }
  93.     private void saveDraw() {
  94.         parent.setDrawingCacheEnabled(true);
  95.         Bitmap b = parent.getDrawingCache();
  96.  
  97.         FileOutputStream fos = null;
  98.         try {
  99.             fos = new FileOutputStream(getFileName());
  100.  
  101.         } catch (FileNotFoundException e) {
  102.             e.printStackTrace();
  103.         }
  104.  
  105.         b.compress(Bitmap.CompressFormat.PNG, 95, fos);
  106.     }
  107.     public File getFileName() {
  108.  
  109.         return new File(Environment.getExternalStorageDirectory()+File.separator+"Download"+File.separator+"SignatureHDX.png");
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement