在Android运动事件上​​画到画布

时间:2013-06-05 16:59:29

标签: android canvas touch-event

我是编程新手,主要是自学和使用其他人在本论坛中提出的问题提供大量帮助的书籍。 我目前正在为傻瓜开发Android游戏开发并遇到了几个问题,其中大多数我已经能够自己解决但是这个编译器错误我无法消失。它'不识别代码中的canvas.drawCircle。我无法弄清楚为什么。 任何帮助或想法非常感谢。 提前致谢 汤姆

public class CrazyEightsView extends View {

    private Paint redPaint;
    private int circleX;
    private int circleY;
    private float radius;

    public  CrazyEightsView(Context context) {
        super(context) ;
        redPaint = new Paint();
        redPaint.setAntiAlias(true);
        redPaint.setColor(Color.rgb(99, 00, 00));
        circleX=100;
        circleY=100;
        radius=30;      
    }

    @Override
    public void onDraw (Canvas canvas) {

    }


    public  boolean onTouchEvent (MotionEvent event) {
        int eventaction = event.getAction();
        int X =(int)event.getX();
        int Y =(int)event.getY();

        switch (eventaction){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                circleX = X;
                circleY = Y;
                break;

                invalidate();
                return true;
        }

        canvas.drawCircle (circleX, circleY, radius, redPaint); 
    }
}

2 个答案:

答案 0 :(得分:1)

作为Raghunandan sais,不要尝试使用onDraw()以外的任何方法。在X中设置YonTouch()值,然后使视图无效。这将再次致电onDraw()

答案 1 :(得分:0)

在draw方法中移动它

      canvas.drawCircle (circleX, circleY, radius, redPaint);

要强制绘制视图,请调用invalidate()。

http://developer.android.com/reference/android/view/View.html#invalidate()

invalidate()将刷新绘图(调用onDraw(画布)。

http://developer.android.com/reference/android/graphics/Canvas.html

根据您的需要修改以下内容。

    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CrazyEightsView ev= new CrazyEightsView(this);

    setContentView(ev);
}

public class CrazyEightsView extends View {

    private Paint redPaint;
    private int circleX;
    private int circleY;
    private float radius;

    public  CrazyEightsView(Context context) {
        super(context) ;
        redPaint = new Paint();
        redPaint.setAntiAlias(true);
        redPaint.setColor(Color.rgb(99, 00, 00));
        circleX=100;
        circleY=100;
        radius=30;

    }
    @Override

    public void onDraw (Canvas canvas) {
                    // draw your circle on the canvas
        canvas.drawCircle (circleX, circleY, radius, redPaint);
    }

    public  boolean onTouchEvent (MotionEvent event) {
            int eventaction = event.getAction();
            int X =(int)event.getX();
            int Y =(int)event.getY();

            switch (eventaction){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                circleX = X;
                circleY = Y;
                invalidate();// call invalidate to refresh the draw
                break;

            }
            return true;
    }
    }

 }

enter image description here