问题描述
- 按下按钮后擦除器没有调用
-
main class: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(); } }); } }
视图类:
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(); } }
布局文件:
<com.authorwjf.GameBoard android:id="@+id/the_canvas" android:layout_width="fill_parent" android:layout_height="fill_parent" />
但是在点击按钮的时候,没有将
mPaint
转换到eraser
解决方案
我看到你在onClick
中创建的GameBoard
没有联系到任何上面。
是不是这个view应该在activity的布局文件中?
时间: 2024-11-03 06:55:00