Draw line on canvas and modify the position after it’s been drawn

Please try this.In this you can draw and edit the line.

class Drawing extends View{
    private Canvas mCanvas = null;
    private Path mPath = null;
    private Paint mBitmapPaint = null;
    private Bitmap mBitmap = null;
    private Bitmap bit=null;
    private Paint mPaint = null;
    private MainActivity baseMainActivity = null;


    public interface onDrawingViewSingleTap{
        void onDrawingViewTap(float x , float y);
    }

    public Drawing(Context c) {
        super(c);
        baseMainActivity=(MainActivity) c;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);//This sets the width of signature

        Display display =baseMainActivity.getWindowManager().getDefaultDisplay();

        mBitmap = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);// 320*480  // For setting size of screen to draw Bitmap

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mCanvas = new Canvas(mBitmap);
        onDraw(mCanvas);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try{
            canvas.drawColor(Color.TRANSPARENT);//Color.WHITE //To change background color of Application
            if(bit!=null)
                canvas.drawBitmap(bit, 0, 0, mBitmapPaint);
            else
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        }catch(Exception e){}
        if(bit==null)
            canvas.drawPath(mPath, mPaint);

        bit=null;

    }
    private float mX, mY;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x+1;
        mY = y+1;

    }

    private void touch_upline(float x,float y) {
        mPath.lineTo(mX, mY);
        mCanvas.drawLine(mX, mY, x, y, mPaint);
        mPath.reset();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            eraseAll();
            touch_upline(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_upline(x,y);
            invalidate();
            break;
        }
        return true;
    }

    public void eraseAll()
    {
        mBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
        mCanvas = new Canvas(mBitmap);
    }

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RelativeLayout relativeLayout= (RelativeLayout) (findViewById(R.id.mainLayout));
        relativeLayout.addView(new Drawing(this));
    }

}

Leave a Comment