表面视图中的触摸事件和手势 [英] Touch events and gestures in surface view

查看:34
本文介绍了表面视图中的触摸事件和手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以为我指出一些课程或对以下案例提出任何建议吗?

Can anybody point me to some classes or suggest anything for the following case?

我有一个带有背景图像的 SurfaceView,我希望在其上绘制其他位图.我想支持以下操作:

I have SurfaceView which has a background image, on top of which I wish to paint other bitmaps. I would like to support following operations:

  • 单击新位图即可添加到背景之上,
  • 在给定位置双击位图被删除,
  • 点击和移动(如拖放)位图正在移动,
  • 按下并移动表面视图正在滚动,
  • 在表面视图中捏合/捏合会相应地缩放.

这仅适用于 GestureRezognizer 吗?如果没有,如何处理所有这些情况?

Is this doable only with GestureRezognizer? If not, how to handle all those cases?

推荐答案

要处理触摸输入,请覆盖扩展 SurfaceView 以处理 MotionEvent 的类中的 onTouchEvent.这是一个示例代码,用于在用户第一次触摸屏幕时获取屏幕位置.

To handle touch input, override onTouchEvent in your class that extends SurfaceView to handle a MotionEvent. Here is a sample code that gets the screen position when a user first touches the screen.

@Override    
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        touchX = event.getX();
        touchY = event.getY();
    }
    return true;
}

有关 MotionEvent 对象的更多信息可以在 Android 开发者网站上找到.

More information about the MotionEvent object can be found on the Android Developers website.

这篇关于表面视图中的触摸事件和手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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