0

i have made a simple draw and erase app in android,I have tried as below,But eraser code is not working...it work as pencil....of strokewidth "5"...Please help me for it.My code is asbelow: in customView

public void eraser() {
        // TODO Auto-generated method stub
        mPaint = new Paint();
        Toast.makeText(getContext(), "eraser", Toast.LENGTH_LONG).show();
        mPaint.setXfermode(null);
        mPaint.setAlpha(0x00FFFFFF);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
       // invalidate();
    }

in mainActivity

eraser.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mDrawView.eraser();

}

});

4

1 回答 1

2

我创建了一个自定义视图,与您的不同。但会运作良好。我已经实施了。

public class SingleTouchView extends View {

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

    public SingleTouchView(Context context, AttributeSet attrs) {
        super(context, attrs);

        /**
         * you need to pass here something width and height if it is predefined,
         * otherwise use explicit constructor to use this view.
         * 
         * I have taken 400 * 400 here. You can get screen height and width and
         * according to that you can pass it.
         */
        mBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        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);
    }

    /**
     * Dynamic View adding with parameters
     * 
     * @param c
     *            : Context
     * @param width
     *            : Width of View
     * @param height
     *            : Height of View
     */
    public SingleTouchView(Context c, int width, int height) {
        super(c);

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

        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);
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        // canvas.drawColor(0xFFAAAAAA);
        canvas.drawColor(0x00FFFFFF);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

    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:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
        }
        return true;
    }

    public void setErase() {
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }
}

我在setErase()这里设置了方法。您可以像以前一样使用它。您可以根据需要添加其他代码。

要显式初始化视图,请使用以下短代码。

SingleTouchView View = new SingleTouchView(this, 480, 800);
relativeLayout..addView(View);

编辑

使用此视图的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnErase"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Erase" />

    <com.example.SingleTouchView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

只需启动一些活动并设置此布局。

于 2013-07-25T09:06:10.610 回答