绘制到canvas onDraw工程,绘制onTouchEvent不会 [英] Drawing to canvas onDraw works, drawing onTouchEvent doesn't

查看:198
本文介绍了绘制到canvas onDraw工程,绘制onTouchEvent不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android SDK中使用了2D图形,我遇到了麻烦,应该是一个简单的例子。



我假设我

  public class DrawView extends View {
Paint paint = new Paint() ;
Canvas();

public DrawView(Context context){
super(context);
paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas){
this.canvas = canvas;
this.canvas.drawLine(0,0,500,500,paint);
}

@Override
public boolean onTouchEvent(MotionEvent event){
Log.d(DrawView,onTouchEvent:+ event.getX ,+ event.getY());

canvas.drawLine(0,500,500,0,paint);

return true;
}
}

上面的代码从 0,0 500,500





问题是第二行没有在触摸事件上绘制。 onTouchEvent 绝对被调用,因为我在日志中看到坐标调试消息。



有人可以指出什么

解决方案

你应该在onTouchEvent()结束时调用invalidate告诉系统更新屏幕。调用invalidate()将调用onDraw()。



此外,基本上错误的是你在这个类中创建一个canvas。这绝对没有什么你。要绘制的画布是您从onDraw()方法获得的画布。 onTouchevent中对canvas.drawLine()的调用不会为你做任何事情,不应该在那里。这是一个空的画布,不是将被发布的那个。

$ on $ touch $ on $ $ $ $ $ $ $ @处理它,如果你需要。你不应该对那里的绘图方法进行任何调用。但是,正如我所说,如果你想触发从onTouchEvent()绘制,你调用invalidate()。如果想根据触摸的位置绘制线条,则需要创建X和Y坐标的类变量。您可以在onTouchEvent()中更新这些X和Y变量,然后在onDraw()中使用它们根据这些X和y变量绘制您需要的任何内容。


I've fooling around with 2D graphics in the Android SDK and I'm having trouble with what should be a simple example.

I'm assuming that I'm just misunderstanding something fundamental/basic.

public class DrawView extends View {
    Paint paint = new Paint();
    Canvas canvas = new Canvas();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
        this.canvas = canvas;
        this.canvas.drawLine(0,0, 500, 500, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("DrawView", "onTouchEvent: " + event.getX() + "," + event.getY() );

        canvas.drawLine(0,500, 500, 0, paint);

        return true;
    }
}

The code above draws a single line from 0,0 to 500,500 when the app start. That parts works just fine.

The issue is that the second line isn't drawn on the touch event. The onTouchEvent is definitely being called because I see the coordinates debug message in the log.

Can someone point out what silly thing I'm doing wrong?

解决方案

You're supposed to call invalidate() at the end of onTouchEvent() to tell the system to update the screen. Calling invalidate() will call onDraw().

Also, what is fundamentally wrong is that you create a canvas in this class you have. That does absolutely nothing for you. The canvas to draw in is the one that you get from the onDraw() method. The call to canvas.drawLine() in onTouchevent isn't doing anything for you and shouldn't be there. That is an empty canvas and isn't the one that will get "posted."

In onTouchEvent() you should only gather the touch event data, and also do some processing on it if you need to. You shouldn't make any calls to drawing methods there. However, as I said, if you want to trigger a draw from onTouchEvent(), you call invalidate(). If you want to draw lines based on where you are touching, you will need to create class variables that are X and Y coordinates. You update these X and Y variables in onTouchEvent(), and then you use them in onDraw() to draw whatever you need based on these X and y variables.

这篇关于绘制到canvas onDraw工程,绘制onTouchEvent不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆