在onTouchEvent Android上更改画布颜色

时间:2015-07-02 07:58:24

标签: android android-canvas

我尝试制作一个活动,当您点按画布时画布颜色会发生变化。

使用我现在的代码,我收到此错误:

  

尝试调用虚方法' void android.graphics.Canvas.drawRect(float,float,float,float,android.graphics.Paint)'在空对象引用上

这是我的活动代码的一部分。

public class ColorActivity extends Activity  {

    private float x = 0;
    private float y = 0;

    public Canvas canvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);

        setContentView(new MyView(this));
    }

    public class MyView extends View {

        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            setFocusableInTouchMode(true);
        }

        @Override
        protected void onDraw(Canvas canvas) {

            super.onDraw(canvas);
            int x = getWidth();
            int y = getHeight();

            Paint paintCanvas = new Paint();
            paintCanvas.setStyle(Paint.Style.FILL);
            paintCanvas.setColor(Color.WHITE);
            paintCanvas.setColor(Color.parseColor("#F44336"));
            canvas.drawRect(0, 0, x, y, paintCanvas);
        }

        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    Paint repaintCanvas = new Paint();
                    repaintCanvas.setStyle(Paint.Style.FILL);
                    repaintCanvas.setColor(Color.WHITE);
                    repaintCanvas.setColor(Color.parseColor("#2196F3"));
                    canvas.drawRect(0, 0, 100, 100, repaintCanvas);

                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.d("LOG","Move");
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d("LOG", "Up");
                    invalidate();
                    break;
            }
            return super.onTouchEvent(event);
        }


    }

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我想我知道你的问题是什么。再次使对onDraw()方法的调用失效,它会覆盖你在onTouchEvent方法中绘制的任何内容。

请尝试以下代码:

public class ColorActivity extends Activity  {

private float x = 0;
private float y = 0;

public Canvas canvas;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color);

    setContentView(new MyView(this));


}

public class MyView extends View {

    Paint paintCanvas;

    public MyView(Context context) {
        super(context);
        paintCanvas = new Paint();
        paintCanvas.setStyle(Paint.Style.FILL);
        paintCanvas.setColor(Color.WHITE);
        paintCanvas.setColor(Color.parseColor("#F44336"));
        // TODO Auto-generated constructor stub
        setFocusableInTouchMode(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        int x = getWidth();
        int y = getHeight();


        canvas.drawRect(0, 0, x, y, paintCanvas);
    }

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:


                paintCanvas.setStyle(Paint.Style.FILL);
                paintCanvas.setColor(Color.WHITE);
                paintCanvas.setColor(Color.parseColor("#2196F3"));

                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("LOG","Move");
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                Log.d("LOG", "Up");
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }


}

答案 1 :(得分:0)

您的Canvas为空。您只能在负责绘制onDraw(Canvas canvas)的{​​{1}}内获取。在Canvas中,设置一些实例变量(例如颜色,位置等)并调用onTouchEvent,然后invalidate()将被触发,您可以根据需要绘制onDraw您将从Canvas中设置的实例值获得的需求。