Advertisement
Ahmed_Zouhir

Canvas / Drawing Code [share & clear]

Dec 11th, 2018
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. //don't forget to like and subscribe in my channel
  2. //you are free to use this code just give credit to my //channel
  3. //my youtube channel https://www.youtube.com/channel/UC85Bsb1LLPD977Zap2HSqpg
  4.  
  5. dv = new DrawingView(this);
  6. linear1.addView(dv);
  7. mPaint = new Paint();
  8. mPaint.setAntiAlias(true);
  9. mPaint.setDither(true);
  10. mPaint.setColor(Color.GREEN);
  11. mPaint.setStyle(Paint.Style.STROKE);
  12. mPaint.setStrokeJoin(Paint.Join.ROUND);
  13. mPaint.setStrokeCap(Paint.Cap.ROUND);
  14. mPaint.setStrokeWidth(12);
  15. }
  16. DrawingView dv; private Paint mPaint;
  17. private Canvas mCanvas;
  18.  
  19. public class DrawingView extends View {
  20. public int width;
  21. public int height;
  22. private Bitmap mBitmap;
  23. private Path mPath;
  24. private Paint mBitmapPaint;
  25. Context context; private Paint circlePaint; private Path circlePath;
  26.  
  27. public DrawingView(Context c) {
  28. super(c);
  29. context=c;
  30. mPath = new Path();
  31. mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  32. circlePaint = new Paint();
  33. circlePath = new Path();
  34. circlePaint.setAntiAlias(true);
  35. circlePaint.setColor(Color.BLUE);
  36. circlePaint.setStyle(Paint.Style.STROKE);
  37. circlePaint.setStrokeJoin(Paint.Join.MITER);
  38. circlePaint.setStrokeWidth(4f);
  39. }
  40.  
  41. @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); }
  42.  
  43. @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
  44. canvas.drawPath( mPath, mPaint); canvas.drawPath( circlePath, circlePaint);
  45. invalidate(); }
  46.  
  47. private float mX, mY;
  48. private static final float TOUCH_TOLERANCE = 4;
  49. private void touch_start(float x, float y) { mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; }
  50.  
  51. private void touch_move(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); mX = x; mY = y; circlePath.reset(); circlePath.addCircle(mX, mY, 30, Path.Direction.CW); } }
  52. private void touch_up() { mPath.lineTo(mX, mY); circlePath.reset();
  53. mCanvas.drawPath(mPath, mPaint);
  54. mPath.reset(); }
  55.  
  56. @Override public boolean onTouchEvent(MotionEvent event) {
  57. float x = event.getX(); float y = event.getY();
  58. switch (event.getAction()) {
  59. case MotionEvent.ACTION_DOWN: touch_start(x, y); invalidate(); break;
  60. case MotionEvent.ACTION_MOVE: touch_move(x, y); invalidate(); break;
  61. case MotionEvent.ACTION_UP: touch_up(); invalidate(); break; } return true; }
  62.  
  63.  
  64.  
  65. //To share add this code above code
  66.  
  67. }
  68. private void storeImage(Bitmap image) { java.io.File pictureFile = new java.io.File(getExternalCacheDir() + "/image.jpg");
  69. if (pictureFile == null) { Log.d("MainActivity", "Error creating media file, check storage permissions: ");
  70. return; } try {
  71. java.io.FileOutputStream fos = new java.io.FileOutputStream(pictureFile); image.compress(Bitmap.CompressFormat.PNG, 90, fos);
  72. fos.close(); } catch (java.io.FileNotFoundException e) { Log.d("MainActivity", "File not found: " + e.getMessage()); } catch (java.io.IOException e) { Log.d("MainActivity", "Error accessing file: " + e.getMessage());
  73. }
  74.  
  75. Intent iten = new Intent(android.content.Intent.ACTION_SEND);
  76. iten.setType("*/*");
  77. iten.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new java.io.File(getExternalCacheDir() + "/image.jpg")));
  78. startActivity(Intent.createChooser(iten, "Send image"));
  79. }
  80.  
  81. private Bitmap getBitmapFromView(View view) { Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
  82. Canvas canvas = new Canvas(returnedBitmap);
  83. android.graphics.drawable.Drawable bgDrawable =view.getBackground();
  84. if (bgDrawable!=null) { bgDrawable.draw(canvas); } else{ canvas.drawColor(Color.WHITE); }
  85. view.draw(canvas);
  86. return returnedBitmap;
  87.  
  88.  
  89. //on the button
  90. storeImage(getBitmapFromView(linear1));
  91.  
  92. //To Clear
  93. mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement