绘画帆布OnDraw中的作品,画的onTouchEvent不 [英] Drawing to canvas onDraw works, drawing onTouchEvent doesn't

查看:196
本文介绍了绘画帆布OnDraw中的作品,画的onTouchEvent不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与2D图形在Android SDK鬼混,我有什么应该是一个简单的例子麻烦。

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;
    }
}

在code以上吸引了来自 0,0 单行 500,500 时,应用程序启动。这部分工作得很好。

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

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

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?

推荐答案

你应该调用无效()在的onTouchEvent()的末尾告诉系统更新屏幕。调用无效()将调用的OnDraw()。

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

此外,什么是根本错误的是,你创建这个类,你有一个画布。这也绝对没有什么给你。画在画布是您从的OnDraw()方法得到的。到canvas.drawLine()中的onTouchEvent调用没有做任何事情你不应该在那里。这是一个空白的画布,是不是会得到一个的网友。

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."

在的onTouchEvent(),你应该只收集触摸事件的数据,并且还做就可以了一些处理,如果您需要。你不应该做任何绘制方法的调用那里。然而,正如我所说,如果要触发的onTouchEvent()一场平局,你调用无效()。如果你想画的基础上,你有动人的线条,你需要创建类变量,这些变量X和Y坐标。您更新这些X和Y变量的onTouchEvent(),然后使用他们的OnDraw()来绘制任何你需要根据这些变量x和y。

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.

这篇关于绘画帆布OnDraw中的作品,画的onTouchEvent不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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