Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pro.izcali.u4drawing;
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- public class MainActivity extends AppCompatActivity {
- RelativeLayout parent;
- CanvasDrawing drawView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (getPermissions()) {
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- parent = (RelativeLayout) findViewById(R.id.canvasDrawing);
- drawView = new CanvasDrawing(this);
- parent.addView(drawView);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- saveDraw();
- Snackbar.make(view, "Guardado exitosamente", Snackbar.LENGTH_LONG)
- .setAction("UNDO", null).show();
- }
- });
- FloatingActionButton fd = (FloatingActionButton) findViewById(R.id.fd);
- fd.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- drawView = new CanvasDrawing(view.getContext());
- parent.addView(drawView);
- }
- });
- }else{
- finish();
- }
- }
- private boolean getPermissions() {
- Boolean granted = false;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
- granted = true;
- }else{
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
- return false;
- }
- } else {
- granted = true;
- }
- return granted;
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
- getPermissions();
- }
- }
- private void saveDraw() {
- parent.setDrawingCacheEnabled(true);
- Bitmap b = parent.getDrawingCache();
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(getFileName());
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- b.compress(Bitmap.CompressFormat.PNG, 95, fos);
- }
- public File getFileName() {
- return new File(Environment.getExternalStorageDirectory()+File.separator+"Download"+File.separator+"SignatureHDX.png");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement