Android – the eraser is not invoked when the button is pressed

I have such main categories:

public class Main extends Activity {

    Button edit,save,eraser,clear;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        edit = (Button) findViewById(R.id.edit);
        save = (Button) findViewById(R.id.save);
        clear = (Button) findViewById(R.id.clear);
        eraser = (Button) findViewById(R.id.eraser);

        eraser.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    GameBoard aa = new GameBoard(Main.this);
                    aa.eraser();
                    aa.invalidate();
                }
        });
    }
}

My view class looks like this:

public class GameBoard extends View{

    public void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        // mPaint.setColor(Color.BLACK);
        mPaint.setColor(0xFFFF0000);

        mPaint.setStyle(Paint.Style.stroke);
        mPaint.setstrokeJoin(Paint.Join.ROUND);
        mPaint.setstrokeCap(Paint.Cap.ROUND);
        mPaint.setstrokeWidth(12);
    }

    public Paint mPaint;

    public GameBoard(Context context) {
        super(context);
        init();
    }

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

    public GameBoard(Context context, AttributeSet aSet) { 
        super(context, aSet); 
        init();
        mBitmap = Bitmap.createBitmap(800, 1024, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

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

    @Override
    public void onDraw(Canvas canvas) {
        // initialize
        // canvas.drawColor(Color.WHITE);
        canvas.drawColor(0xFFAAAAAA);
        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 eraser() {
        // TODO Auto-generated method stub
        Toast.makeText(getContext(), "eraser", Toast.LENGTH_LONG).show();
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        invalidate();
    }
}

This is the layout:

<com.authorwjf.GameBoard
    android:id="@+id/the_canvas"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

When I click a button, it does not change mpaint to eraser

Who can help me?

resolvent:

As far as I know, the gameboard you created in onclick will never be attached to anything

Does this view belong to the layout of an activity?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>