SachinKS 2013-02-21 05:48 采纳率: 0%
浏览 2578
已采纳

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

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 2013-02-21 06:05
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题