gpt4 book ai didi

android - 也可以使用按钮滑动和更改图像

转载 作者:太空狗 更新时间:2023-10-29 15:54:08 27 4
gpt4 key购买 nike

我正在尝试制作一个图库应用程序。我有两个按钮用于“下一个”和“后退”,当我按下一个按钮时,我需要显示下一个图像,我还需要通过滑动来更改图像。我试过图像适配器。但我不知道用按钮更改图像。

最佳答案

您可以创建新类来检测滑动并创建即时使用它。

public class SwipeDetect implements OnTouchListener {

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

public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}

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();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
} }

使用它:

YourImageView.setOnTouchListener(new SwipeDetect() {
public void onSwipeRight() {
//Your code here
}

public void onSwipeLeft() {
//Your code here
}
});

您可以将它用于 ImageView 、布局...

关于android - 也可以使用按钮滑动和更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359206/

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