gpt4 book ai didi

android - 如何处理 ImageView 上的向下滑动手势

转载 作者:行者123 更新时间:2023-11-30 03:40:58 25 4
gpt4 key购买 nike

我正在尝试识别 ImageView 上的向下滑动手势。我试过 GestureDetector 但现在已弃用。我正在使用的示例代码如下。但是,它不会进入 onFling() 方法。

GestureDetector gestureDetector;
gestureDetector = new GestureDetector(new GestureListener());

view.findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
System.out.println("DONE-------");
updateSwipeAction();
return true;
}
return false;
}
});

class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 10;
private static final int SWIPE_THRESHOLD_VELOCITY = 10;

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
//From Right to Left
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
//From Left to Right
return true;
}

if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
//From Bottom to Top
return true;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
//From Top to Bottom
return true;
}
return false;
}

@Override
public boolean onDown(MotionEvent e) {
//always return true since all gestures always begin with onDown and<br>
//if this returns false, the framework won't try to pick up onFling for example.
return true;
}
}

最佳答案

试试这个,实现 ongestureListener 并且有一个你可以直接访问的 onscroll 方法

@SuppressLint("NewApi")

public class AndroidExplorer extends Activity implements    
android.view.GestureDetector.OnGestureListener

{
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle mybundle)
{
// TODO Auto-generated method stub
super.onCreate(mybundle);
setContentView(R.layout.main);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}




}

关于android - 如何处理 ImageView 上的向下滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15770887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com