我不会'知道如何将onTouchListener连接到SimpleOnGestureListener,因为onTouchlistener只接受它自己的类型 - touchlistener。
我这样做的方式是:onTouchEvent(不是特定的imageView' onTouch)将事件提供给gestureDetector对象进行处理。
这会导致活动能够检测到fling,doubletap和其他简单的手势。
但我只想要特定的imageView来响应并识别doubleTap
public class MainActivity extends Activity {
private class Erjan_gestures extends SimpleOnGestureListener{
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.wtf("x", "double tap occured!");
return true ;
}
}
Button button;
ImageView image ;
GestureDetector gestureDetector;
int oldW , oldH ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.card);
button = (Button) findViewById(R.id.button);
oldW = 500;
oldH = 600;
gestureDetector = new GestureDetector(new Erjan_gestures());
gestureDetector.setIsLongpressEnabled(true);
image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.wtf("x", "card's onTouch");
//HOW TO CONNECT this ontouch to simple gesturelistener(class above)?
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event){
Log.wtf("x", "-------------------------------onTouchEvent(MotionEvent event)");
return gestureDetector.onTouchEvent(event) ;
}
}
如何连接图像onTouch以识别简单的手势?
答案 0 :(得分:0)
我发现没有比检查motionEvent的坐标更好的解决方案:
private class Erjan_gestures extends SimpleOnGestureListener{
@Override
public boolean onDoubleTap(MotionEvent e) {
float e_x = e.getRawX();
float e_y = e.getRawY();
if(e_x > 1360 && e_x < 1733 && e_y > 465 && e_y < 1028){
doubleTapHappened = true ;//DESIRED ACTION
Log.wtf("x", "double tap occured on CARD!");
}
return true ;
}
我还无法编写自己的自定义视图,以期望的方式响应手势。所以我只检查点击发生的位置。