Advertisement
urksiful

Canvas Drawing .java

May 17th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package pro.izcali.u4drawing;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Path;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11.  
  12. /**
  13.  * Created by urksi on 31/10/2017.
  14.  */
  15.  
  16. public class CanvasDrawing extends View {
  17.  
  18.     private Bitmap mBitmap;
  19.     private Canvas mCanvas;
  20.     private Path mPath;
  21.     private Paint mBitmapPaint;
  22.     private Paint   mPaint;
  23.  
  24.     public CanvasDrawing (Context c) {
  25.         super(c);
  26.  
  27.         mPath = new Path();
  28.         mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  29.  
  30.         mPaint = new Paint();
  31.         mPaint.setAntiAlias(true);
  32.         mPaint.setDither(true);
  33.         mPaint.setColor(0xFF000000);
  34.         mPaint.setStyle(Paint.Style.STROKE);
  35.         mPaint.setStrokeJoin(Paint.Join.ROUND);
  36.         mPaint.setStrokeCap(Paint.Cap.ROUND);
  37.         mPaint.setStrokeWidth(3);
  38.     }
  39.  
  40.     @Override
  41.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  42.         super.onSizeChanged(w, h, oldw, oldh);
  43.         mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  44.         mCanvas = new Canvas(mBitmap);
  45.     }
  46.  
  47.     @Override
  48.     protected void onDraw(Canvas canvas) {
  49.         canvas.drawColor(Color.WHITE);
  50.         canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
  51.         canvas.drawPath(mPath, mPaint);
  52.     }
  53.  
  54.  
  55.  
  56.     private float mX, mY;
  57.     private static final float TOUCH_TOLERANCE = 4;
  58.  
  59.     private void touch_start(float x, float y) {
  60.         mPath.reset();
  61.         mPath.moveTo(x, y);
  62.         mX = x;
  63.         mY = y;
  64.     }
  65.     private void touch_move(float x, float y) {
  66.         float dx = Math.abs(x - mX);
  67.         float dy = Math.abs(y - mY);
  68.         if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
  69.             mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
  70.             mX = x;
  71.             mY = y;
  72.         }
  73.     }
  74.     private void touch_up() {
  75.         mPath.lineTo(mX, mY);
  76.         // commit the path to our offscreen
  77.         mCanvas.drawPath(mPath, mPaint);
  78.         // kill this so we don't double draw
  79.         mPath.reset();
  80.     }
  81.  
  82.     @Override
  83.     public boolean onTouchEvent(MotionEvent event) {
  84.         float x = event.getX();
  85.         float y = event.getY();
  86.  
  87.         switch (event.getAction()) {
  88.             case MotionEvent.ACTION_DOWN:
  89.                 touch_start(x, y);
  90.                 invalidate();
  91.                 break;
  92.             case MotionEvent.ACTION_MOVE:
  93.                 touch_move(x, y);
  94.                 invalidate();
  95.                 break;
  96.             case MotionEvent.ACTION_UP:
  97.                 touch_up();
  98.                 invalidate();
  99.                 break;
  100.         }
  101.         return true;
  102.     }
  103.  
  104.     public void clear(){
  105.         mBitmap.eraseColor(Color.TRANSPARENT);
  106.         invalidate();
  107.         System.gc();
  108.     }
  109.  
  110.     public void setBackground(int white) {
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement