【问题标题】:Image swiping and changing with buttons as well也可以使用按钮滑动和更改图像
【发布时间】:2013-06-28 07:12:05
【问题描述】:

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

【问题讨论】:

    标签: android android-viewpager android-imageview


    【解决方案1】:

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

    公共类 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() {
    } }
    

    使用它:

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

    您可以将它用于图像视图、布局...

    【讨论】:

      【解决方案2】:

      尝试使用this教程供您参考,以实现按钮库

      在本教程中,我将箭头按钮用于下一个上一个

      我在本教程中实现了更多功能。

      喜欢:

      拇指全图在屏幕中查看。

      从您选择突出显示的缩略图中。

      通过箭头按钮,您还可以更改图像(下一个/上一个)。

      【讨论】:

        【解决方案3】:

        尝试以this 为起点。你也可以实现here所示的教程。

        【讨论】:

          猜你喜欢
          • 2011-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多