スワイプアクションをリッスンするメソッドでonSwipeListenerクラスを挿入できます。次に、アクティビティのレイアウト(LinearLayout/RelativeLayout)にview.OnTouchListenerを設定し、onSwipeListenerのさまざまなメソッドをオーバーライドして、さまざまなタスクを挿入できます。
以下は、作成できるonSwipeListenerクラスです。次のように
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
result = true;
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
result = true;
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
このクラスを作成した後、あなたはそれを呼び出すことができます。
relativeLayout.setOnTouchListener(new OnSwipeTouchListener(context) {
public void onSwipeRight() {
//do something
}
public void onSwipeLeft() {
//do something
}
}
ホープ、このことができます!
以下のリンクは、画像を使用して行う方法を説明しています。http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-2-building -the-touch-example/1763誰かが似たような質問をしました:http://stackoverflow.com/questions/5648985/ontouchlistener-for-entire-screenジェスチャーリスナーの実装に関しては、私は分かりません。 – ihtkwot
すべてのビューで透明なオーバーレイビューを作成し、オーバーレイビューを聞くことは可能ですか?このオーバレイビューをクリックすると、クリックはボタンのような下のビューに移動しますか? –
私はマイクについては分かりません。私は自分自身を試していない。私はちょうど私があなたの質問への答えを探している間に見つけたいくつかのアプローチを一緒に召集しようとしました。申し訳ありませんが、私はもっと役に立ちませんでした。 – ihtkwot