Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing and erasing line in android

Tags:

android

How to draw a line (not a straight one just random continuous point(like we draw with pencil)) and then to erase it.


1 Answers

it is finger painting application

////////////******************* Pinting view *******************///////////////////
public class MyView extends View {
    int bh = originalBitmap.getHeight();
    int bw = originalBitmap.getWidth(); 

    public MyView (Context c)  {  
        super(c);
        //mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
        mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);           
            /*mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);*/
    }
    @Override 
    protected void onDraw(Canvas canvas) {   
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }
    ////////************touching evants for painting**************///////
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    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;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        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:
                //if(mode == DRAW) { mView.setOnTouchListener((OnTouchListener) this); }
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }  //end of touch events for image
}// end MyView  

for erase

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
like image 137
RajaReddy PolamReddy Avatar answered Oct 21 '25 03:10

RajaReddy PolamReddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!