简体   繁体   English

如何在基本四个方向(即右,左,上,下)上滑动视图中的图像

[英]how to swipe an image in a view in basic four directions i.e. right, left, up, down

I want to develop my own Accept and Decline buttons by swiping an image to right and left respectively and by swipe up and down want to open templates.I m new to android so i have no idea how to do this. 我想通过分别向右和向左滑动图像以及向上和向下滑动以打开模板来开发自己的“接受”和“拒绝”按钮。我是android的新手,所以我不知道该怎么做。

Is there any way to make this? 有什么办法做到这一点? Any hint? 有什么提示吗?

I hope to be clear. 我希望清楚。

You can use an OnTouchListener , and compute directions. 您可以使用OnTouchListener并计算方向。

Based on this answer : 基于此答案

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

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();

                // Move the ImageView here, from diffX pixels horizontally and diffY pixels vertically

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测左/右和上/下之间的滑动方向 - How to detect swipe direction between left/right and up/down 左右滑动时动作 - On swipe left, right, up, down action 在缩放或拖动(左,右,上,下)图像时,如何设置另一个视图的精确位置? - How to set exactly position another view when zoom or drag (left, right, up, down) image? 根据速度或其他变量向左、向右、向上和向下滑动 - swipe left, right, up and down depending on velocity or other variables Android GestureDetector向上/向下滑动与向左/向右滑动 - Android GestureDetector Swipe Up/Down vs. Left/Right 全方位滑动手势(向左,向右,向上,向下) - Swipe gesture in all direction(Left,Right, Up, Down ) 通过向内和向外滑动(向上,向左,向右,向下)更改Android动画图片 - Android animation picture change with swipe in and out (up,left,right,down) 如何根据玩家的需要上,下,左或右移动图像? - how to move an image up, down, left or right as the player wants? Google眼镜:借助swipe_left和swipe_right上下滚动 - Google glass: Scroll up and down thanks to swipe_left and swipe_right 如何结合事件“setOnClickListener”和“向右滑动”或“向左滑动”? - How can I combine the events “setOnClickListener” and “swipe to the right” or “swipe to the left”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM