gpt4 book ai didi

Android OnGestureListener Fling 未检测到

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:24 25 4
gpt4 key购买 nike

我想检测屏幕 block 中的快速运动。我为此使用以下代码。

public class MyinfoActivity extends Activity implements OnGestureListener {
@Override
public void onCreate(Bundle savedInstanceState) {
..........
..........
gestureScanner = new GestureDetector(this);
resBlock = (TableRow) findViewById(R.id.ResBlock);
gestureScanner = new GestureDetector(this);
}

@Override
public boolean onTouchEvent(MotionEvent me){
Log.d(null,"Touch");
if (gestureScanner.onTouchEvent(me))
return gestureScanner.onTouchEvent(me);
else
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
Log.d(null,"Fling");
............
............
}

@Override
public boolean onDown(MotionEvent arg0) {
return false;
}

@Override
public void onLongPress(MotionEvent e) {}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}

@Override
public void onShowPress(MotionEvent e) {}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}

它正在检测 TouchEvent,但未检测到任何滑动 Action 。我的代码有什么问题?

最佳答案

我使用了下面的代码并解决了这个问题。

public class MyinfoActivity extends Activity {
private GestureDetector gestureScanner;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myinfotrackerinner);
gestureScanner = new GestureDetector(this,simpleOnGestureListener);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureScanner.onTouchEvent(event);
}

GestureDetector.SimpleOnGestureListener simpleOnGestureListener =
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent event) {
return true;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(null,"Fling");
int dx = (int) (event2.getX() - event1.getX());
// don't accept the fling if it's too short
// as it may conflict with a button push
if (Math.abs(dx) > MAJOR_MOVE
&& Math.abs(velocityX) > Math.abs(velocityY)) {
if (velocityX > 0) {
moveGraph("L");
} else {
moveGraph("R");
}
return true;
} else {
return false;
}
}
};
}

关于Android OnGestureListener Fling 未检测到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5309944/

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