图像滑动和更改按钮也是如此

dev*_*ian 2 android android-imageview android-viewpager

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

小智 7

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

公共类SwipeDetect实现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() {
} }
Run Code Online (Sandbox Code Playgroud)

用它:

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

            public void onSwipeLeft() {
                //Your code here
            }
        });
Run Code Online (Sandbox Code Playgroud)

你可以用它来进行imageview,布局......