gpt4 book ai didi

android - Android 中 ImageView 上的滑动(fling)手势识别问题

转载 作者:行者123 更新时间:2023-11-29 22:19:50 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序的 ImageView 上使用简单的滑动/滑动手势识别器。但是,我什么也没看到。任何人都可以看看代码并让我知道我在这里缺少什么吗?谢谢。

注意 1:从我的调试中,我可以看到执行确实进入了 GestureListener 内部。但是,它会从那里返回 false。

代码:

public class ViewSuccess extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
ImageView movieFrame;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test);



imageLoader=new ImageLoader(getApplicationContext());




movieFrame = (ImageView) findViewById(R.id.widget32);
movieFrame.setImageResource(R.drawable.sample);





// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};

movieFrame.setOnTouchListener(gestureListener);


class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

System.out.println("in gesture recognizer !!!");

try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(ViewSuccess.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(ViewSuccess.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}

最佳答案

开始工作....我们需要将以下方法添加到 SimpleOnGestureListener 类中

 @Override
public boolean onDown(MotionEvent e)
{
return true;
}

关于android - Android 中 ImageView 上的滑动(fling)手势识别问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535612/

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