gpt4 book ai didi

android - 如何通过移动手指来改变 Activity ?

转载 作者:太空狗 更新时间:2023-10-29 16:35:46 25 4
gpt4 key购买 nike

我想寻求帮助。你能给我一个如何通过移动手指改变 Activity 的建议吗?(如图所示)谢谢解答

enter image description here

图片:https://imageshack.us/download/661/xaxrzF.png

最佳答案

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;
private SwipeGestureInterface gestureInterface;
private boolean left, right, top, bottom;
private boolean doubleTap;
private int x, y;

public OnSwipeTouchListener(Context ctx, boolean left, boolean right,
boolean top, boolean bottom, boolean doubleTap,
SwipeGestureInterface gestureInterface) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
this.gestureInterface = gestureInterface;
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.doubleTap = doubleTap;
}

private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 120;

@Override
public boolean onDown(MotionEvent e) {
setX((int) e.getX());
setY((int) e.getY());
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();
}

Utils.log("onfling", "val Y:" + velocityY);
return result;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
if (gestureInterface != null && doubleTap) {
gestureInterface.onDoubleTap();
return true;
}
return false;
}

}

public void onSwipeRight() {
if (gestureInterface != null && right)
gestureInterface.onSwipeRight();
}

public void onSwipeLeft() {
if (gestureInterface != null && left)
gestureInterface.onSwipeLeft();
}

public void onSwipeTop() {
if (gestureInterface != null && top)
gestureInterface.onSwipeTop();
}

public void onSwipeBottom() {
if (gestureInterface != null && bottom)
gestureInterface.onSwipeBottom();
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
gestureDetector.onTouchEvent(event);
return false;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

关于android - 如何通过移动手指来改变 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29253706/

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