2 sachinks SachinKS 于 2013.02.21 13:48 提问

按下按钮后擦除器没有调用

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

1个回答

Kill_it
Kill_it   2013.02.21 14:05
已采纳

我看到你在onClick中创建的GameBoard没有联系到任何上面。

是不是这个view应该在activity的布局文件中?

SachinKS
SachinKS 太谢谢了~~
4 年多之前 回复
Kill_it
Kill_it GameBoard gameBoard = (GameBoard)findViewById(R.id.view_gameboard); 在你的`GameBoard XML element有 android:id="@+id/view.gameboard" 设置
4 年多之前 回复
SachinKS
SachinKS 怎么用findViewByID()?
4 年多之前 回复
Kill_it
Kill_it 这样的话,你应该用findViewById()获取 GameBoard对象。不需要创建新的。
4 年多之前 回复
SachinKS
SachinKS 是在一个main XML文件中的
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片