我有这样的主要类:
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更改为橡皮擦.
谁能帮我?
解决方法:
据我所知,你在onClick中创建的GameBoard永远不会附加到任何东西.
该视图是否属于Activity的布局?
总结以上是内存溢出为你收集整理的android – 按下按钮时不调用橡皮擦全部内容,希望文章能够帮你解决android – 按下按钮时不调用橡皮擦所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)