Touch events and gestures in surface view

倾然丶 夕夏残阳落幕 提交于 2019-12-10 23:33:02

问题


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

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:

  • on single tap new bitmap is added on top of background,
  • on double tap bitmap at given position is removed,
  • on tap&move (like drag&drop) bitmap is moving,
  • on press and move surface view is scrolling,
  • on pinch out/pinch in surface view is scaling accordingly.

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


回答1:


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

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



来源:https://stackoverflow.com/questions/15862705/touch-events-and-gestures-in-surface-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!