添加触摸事件监听到Android帆布 [英] add touch event listener to android canvas

查看:113
本文介绍了添加触摸事件监听到Android帆布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个画布对象(C),我需要添加一个触摸事件侦听器,这样,每当画布感动,我可以调用一个函数。我一直没能找到如何将它添加上,到目前为止,这是我有:

I have a canvas object (c) , and I need to add a touch event listener so that whenever the canvas is touched, I can call a function. I haven't been able to find how to add it on so far, Here's what I have:

c.setOnTouchListener(new onTouchListener(){onTouchEvent()});

然后onTouchEvent方法:

and then the onTouchEvent method:

public boolean onTouchEvent(MotionEvent e){
    addBubble();
    return false;
}

新Java和Android的发展,感谢您的帮助!

new to both java and android development, thanks for any help!

如果我不是为了一个触摸监听器添加到画布上,那么应该怎么做到这一点?

If i'm not meant to add a touch listener to a canvas, then how should I achieve this?

推荐答案

画布不要触摸事件。画布不在屏幕元素,它们是通用的绘制区域,有点像HDC在Win32中。他们甚至不需要画到屏幕上,就可以得出一个位图在内存中。视图是屏幕上的元素,他们有接触的听众。您将需要监听器添加到视图,而不是一个画布。

Canvases don't get touch events. Canvases aren't on screen elements, they're generic drawable areas, sort of like HDC in Win32. They don't even need to draw to the screen, they can draw to a bitmap in memory. Views are the on screen elements, and they have the touch listeners. You would need to add the listener to the view, not to a canvas.

这篇关于添加触摸事件监听到Android帆布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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