更新时间:2023-01-25 14:05:31
此示例显示如何向视图添加手势检测器.布局只是一个Activity内部的单个View
.您可以使用相同的方法将手势检测器添加到任何类型的视图中.
This example shows how to add a gesture detector to a view. The layout is just a single View
inside of an Activity. You can use the same method to add a gesture detector to any type of view.
我们将手势检测器添加到绿色的View
.
We will add the gesture detector to the green View
.
基本思想是添加 OnTouchListener
到视图.通常,我们会在此处获取所有原始触摸数据(例如ACTION_DOWN
,ACTION_MOVE
,ACTION_UP
等),但是与其自己处理,不如将其转发给手势检测器以对手势进行解释.触摸数据.
The basic idea is to add an OnTouchListener
to the view. Normally we would get all the raw touch data here (like ACTION_DOWN
, ACTION_MOVE
, ACTION_UP
, etc.), but instead of handling it ourselves, we will forward it on to a gesture detector to do the interpretation of the touch data.
我们正在使用 SimpleOnGestureListener
.关于此手势检测器的好处是,我们只需要覆盖所需的手势即可.在这里的示例中,我包括了很多.您可以删除不需要的内容. (不过,您应该始终在onDown()
中返回true
.返回true表示我们正在处理事件.返回false会使系统停止提供更多触摸事件.)
We are using a SimpleOnGestureListener
. The nice thing about this gesture detector is that we only need to override the gestures that we need. In the example here I included a lot of them. You can remove the ones you don't need. (You should always return true
in onDown()
, though. Returning true means that we are handling the event. Returning false will make the system stop giving us any more touch events.)
public class MainActivity extends AppCompatActivity {
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is the view we will add the gesture detector to
View myView = findViewById(R.id.my_view);
// get the gesture detector
mDetector = new GestureDetector(this, new MyGestureListener());
// Add a touch listener to the view
// The touch listener passes all its events on to the gesture detector
myView.setOnTouchListener(touchListener);
}
// This touch listener passes everything on to the gesture detector.
// That saves us the trouble of interpreting the raw touch events
// ourselves.
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// pass the events to the gesture detector
// a return value of true means the detector is handling it
// a return value of false means the detector didn't
// recognize the event
return mDetector.onTouchEvent(event);
}
};
// In the SimpleOnGestureListener subclass you should override
// onDown and any other gesture that you want to detect.
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d("TAG","onDown: ");
// don't return false here or else none of the other
// gestures will work
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("TAG", "onSingleTapConfirmed: ");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("TAG", "onLongPress: ");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("TAG", "onDoubleTap: ");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("TAG", "onScroll: ");
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d("TAG", "onFling: ");
return true;
}
}
}
这是运行此项目的快速设置,所以我建议您尝试一下.请注意日志事件的发生方式和时间.
It is a quick setup to run this project, so I recommend you try it out. Notice how and when the log events occur.