onTouch()无法绘制画布

时间:2013-05-10 03:14:52

标签: android canvas draw

onDraw(Canvas canvas)自动运行。在此视图中,onTouch()方法侦听触摸屏幕。我点击屏幕时需要重绘矩形,但不是绘制矩形画布。

public class Viewer extends View implements OnTouchListener{

    public Viewer(Context context) {
        super(context);

        .............
        ..............
        this.setOnTouchListener(this);              
    }

    @Override
    public void onDraw(Canvas canvas){  
        super .onDraw(canvas);
        drawBackGround(imgObj.bg01, canvas);    
    }

    @Override
    public boolean onTouch(View v,MotionEvent event) {
        // TODO Auto-generated method stub      

        Canvas c = new canvas();
        c.drawRect(.......);    //<------ It's work but it isn't redraw
        return true;
    }

1 个答案:

答案 0 :(得分:0)

试试这个,

Canvas mCanvas;
 @Override
    public void onDraw(Canvas canvas){  

        mCanvas = canvas;
        drawBackGround(imgObj.bg01, mCanvas);
        super.onDraw(mCanvas);

    }

    @Override
    public boolean onTouch(View v,MotionEvent event) {
        // TODO Auto-generated method stub      

        mCanvas.drawRect(.......); 
        invalidate();
        return true;
    }