使位图监听触摸事件

发布于 2024-09-07 05:35:29 字数 158 浏览 3 评论 0原文

如何向简单的位图添加触摸功能?

我尝试为位图创建一个包装类,
实现了 OnGestureListener
但没有成功。

我想避免扩展 View 类来实现此目的。

谢谢!

How can I add touch capabilities to a simple bitmap?

I've tried to create a wrapper class for a bitmap,
which implemented OnGestureListener,
but it didn't work.

I want to avoid extending View class to achieve this.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不疑不惑不回忆 2024-09-14 05:35:29

位图本身只是图像的表示...因此,为了显示它,您必须将其绘制在某个地方(一个视图,事实上您总是将其绘制在View上)。然后,就无法避免使用 View 类,因为所有用户界面小部件都扩展了它。

总之,如果您想简单地将触摸侦听器设置为单个位图,例如,您可以将其绘制在 ImageView 上并设置适当的侦听器。另一方面,如果您在某处绘制了一组位图(例如,在 SurfaceView 上),则应通过其坐标来定位位图(在这种情况下,View接收事件的 code> 将会是 SurfaceView)。

A Bitmap per se is just a representation of an image... thus, in order to show it you will have to draw it somewhere (a View, in fact you always draw it on a View). Then, there's no way to avoid using the View class since all user interface widgets extend it.

In conclusion, if you want to simply set touch listeners to a single Bitmap you can, for instance, draw it on a ImageView and set the appropriate listeners. On the other hand, if you have a set of bitmaps drawn somewhere (for instance, on a SurfaceView), you should locate the bitmap by its coordinates (in that case, the View which would receive the events will be the SurfaceView).

〃温暖了心ぐ 2024-09-14 05:35:29

您是否已将 OnGestureListener 实现连接到 GestureDetector? GestureDetector 分析 MotionEvent 并根据其发现的运动类型在侦听器上调用适当的回调。

public class MyActivity extends Activity {
    private GestureDetector detector;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...
        detector = new GestureDetector(new MyGestureListener());
        ...
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}

Do you have your OnGestureListener implementation wired to a GestureDetector? The GestureDetector analyzes a MotionEvent and invokes the appropriate callback on the listener based on the type of movement it found.

public class MyActivity extends Activity {
    private GestureDetector detector;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...
        detector = new GestureDetector(new MyGestureListener());
        ...
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文