1

I am new to android,i prepared one painting view using finger with help of FingerPaint.java in Api Demo.It is working fine.

The same view contains one button(clear),if we click on button paint will be clear(empty sceen and one button).My problem is how to clear the paint.My view code is,

public class MyDraw extends View {

    Paint mPaintAlphabet = new Paint();

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    float x, y;
    float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private Bitmap mBitmap;
    Canvas mCanvas;
    Path mPath;
    Paint mBitmapPaint;

    public MyDraw(Context context) {
        super(context);

        mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);


    }

    public MyDraw(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        mBitmap = Bitmap.createBitmap(480, 480, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        mPaintAlphabet.setDither(true);
        mPaintAlphabet.setColor(0xFFFF0000);
        mPaintAlphabet.setStyle(Paint.Style.STROKE);
        mPaintAlphabet.setStrokeJoin(Paint.Join.ROUND);
        mPaintAlphabet.setStrokeCap(Paint.Cap.ROUND);
        mPaintAlphabet.setStrokeWidth(3);
        mPaintAlphabet.setTextSize(350);

    }

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

    public void onDraw(Canvas canvas) {

            if (MyAlphabetsActivity.mArryLstAlphabets.get(
                MyAlphabetsActivity.mAlphaIndex).equals("I")) {

            canvas.drawText((String) MyAlphabetsActivity.mArryLstAlphabets
                    .get(MyAlphabetsActivity.mAlphaIndex), 180, 300,
                    mPaintAlphabet);

        } else if (MyAlphabetsActivity.mArryLstAlphabets.get(
                MyAlphabetsActivity.mAlphaIndex).equals("J")) {

            canvas.drawText((String) MyAlphabetsActivity.mArryLstAlphabets
                    .get(MyAlphabetsActivity.mAlphaIndex), 180, 300,
                    mPaintAlphabet);

        } else {
            canvas.drawText((String) MyAlphabetsActivity.mArryLstAlphabets
                    .get(MyAlphabetsActivity.mAlphaIndex), 120, 300,
                    mPaintAlphabet);
        }

        System.out.println("in on draw");
        System.out.println("path is----" + mPath);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, MyAlphabetsActivity.mPaint);
    }

    private void touch_start(float x, float y) {
        mPath.reset();
        System.out.println("mPath-----"+mPath);
        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, MyAlphabetsActivity.mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();

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


}

And my Activity code is,

public class MyAnimViewActivity extends Activity
         {    
    public static Paint       mPaint;
    private MaskFilter  mEmboss;
    private MaskFilter  mBlur;
    Button mBtnClear;
    MyDraw mMyDraw=new MyDraw();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyOwnView(this));

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                       0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

        mBtnClear=(Button)findViewById(R.id.btnClear);

        mBtnClear.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.btnClear:
                    mMyDraw.mPath.reset();

                    break;

                }
            }
        });
    }
}

Please help me..Thanks in advance

SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46

1 Answers1

2

make a public function in your custom drawing class named as clearDrawing(); and write this:

mBitmap = null;
mPath = null;
mBitmap = Bitmap.createBitmap(480, 480, Bitmap.Config.ARGB_8888);
mPath = new Path();

now call clearDrawing() to reset drawing control.

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54
  • This works, but any preceding drawings are deleted when the user lifts their finger off the screen. Anyway I can prevent this? Thanks – Stefan Dunn Mar 31 '12 at 12:34
  • yes they can be prevented.. Put you drawings in an arraylist and in onDraw function draw everything using loop. – Awais Tariq Apr 02 '12 at 05:23
  • Hi,Awais Tariq ...can you please post any example for above issue.In My requirement is, after draw a line ,The draw line should be clear after few secns..can you please tell me How can it is possible – bhargavkumar040 Aug 30 '12 at 12:46
  • It works fine but we not able to draw again.When we trying to draw it get disappear.Please advise. – Umesh Oct 15 '13 at 14:22