做一个视图滑动式和活动 [英] Make a view swipeable and movable

查看:135
本文介绍了做一个视图滑动式和活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在成功地移动我的看法与此code

 公共布尔onTouch(查看视图,MotionEvent事件){
        最终诠释X =(INT)event.getRawX();
        最终诠释Y =(int)的event.getRawY();
        开关(event.getAction()及MotionEvent.ACTION_MASK){
            案例MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams =(RelativeLayout.LayoutParams)view.getLayoutParams();
                xDelta = X - lParams.leftMargin;
                yDelta = Y - lParams.topMargin;
                打破;
            案例MotionEvent.ACTION_UP:
                打破;
            案例MotionEvent.ACTION_POINTER_DOWN:
                打破;
            案例MotionEvent.ACTION_POINTER_UP:
                打破;
            案例MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams的LayoutParams =(RelativeLayout.LayoutParams)view.getLayoutParams();
                layoutParams.leftMargin = X - xDelta;
                layoutParams.topMargin = Y - yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(的LayoutParams);
                打破;
        }
        root.invalidate();
        返回true;
    }

如果我美元的视点P $ PSS和移动我的手指,视图跟随它,现在我想实现滑动手势,它应该像这样工作:
如果我刷从背景开始,打的看法,我想它基于哪个方向轻扫被(上,下,左或右)扮演的旋转动画。
我还是想preserve移动视图的能力。

想象一下,一个硬币,我可以走动,如果我的手指扔它,它使一个360°旋转

编辑:
这里是我的code旋转的观点是如何工作的,但如果我从视图里面有被旋转启动它只能

 公共类OnSwipeTouchListener实现OnTouchListener {    公众最终GestureDetector gestureDetector;    公共OnSwipeTouchListener(上下文CTX){
        gestureDetector =新GestureDetector(CTX,新GestureListener());
    }    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        返回false;
    }    私人final类GestureListener扩展SimpleOnGestureListener {        私有静态最终诠释SWIPE_THRESHOLD = 100;
        私有静态最终诠释SWIPE_VELOCITY_THRESHOLD = 100;        @覆盖
        公共布尔onDown(MotionEvent五){
            返回true;
        }        @覆盖
        公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){            布尔结果= FALSE;
            尝试{
                浮diffY = e2.getY() - e1.getY();
                浮diffX = e2.getX() - e1.getX();
                如果(Math.abs(diffX)GT; Math.abs(diffY)){
                    如果(Math.abs(diffX)GT; SWIPE_THRESHOLD和放大器;&安培; Math.abs(velocityX)GT; SWIPE_VELOCITY_THRESHOLD){
                        如果(diffX大于0){
                            onSwipeRight();
                        }其他{
                            onSwipeLeft();
                        }
                    }
                }其他{
                    如果(Math.abs(diffY)GT; SWIPE_THRESHOLD和放大器;&安培; Math.abs(velocityY)GT; SWIPE_VELOCITY_THRESHOLD){
                        如果(diffY大于0){
                            onSwipeBottom();
                        }其他{
                            onSwipeTop();
                        }
                    }
                }
            }赶上(例外的例外){
                exception.printStackTrace();
            }
            返回结果;
        }
    }    公共无效onSwipeRight(){
    }    公共无效onSwipeLeft(){
    }    公共无效onSwipeTop(){
    }    公共无效onSwipeBottom(){
    }
}


解决方案

您应该重写 OnTouch()在你的RelativeLayout并通过MotionEvent到一个<一个href=\"http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html\"相对=nofollow> SimpleOnGestureListener 。在那里,你可以为所有的方法返回false,除了<一个href=\"http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#onFling%28android.view.MotionEvent,%20android.view.MotionEvent,%20float,%20float%29\"相对=nofollow> onFling()在您检查的第一个事件是否是你要旋转,第二个事件是你要旋转视图内视图之外。如果事件是不是,你返回false,表示你不处理该事件,否则你旋转的硬币,并表明您通过返回true处理的触摸事件。

作为额外的一点,我不认为你应该在右侧和底部页边距设置为-250,否则视图将扩大到250像素过去的权利,我希望你想让它只是你的RelativeLayout的底部边缘分别延伸到该视图的预定宽度和高度。

I'm currently successfully moving my view with this code

public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                xDelta = X - lParams.leftMargin;
                yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - xDelta;
                layoutParams.topMargin = Y - yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
        }
        root.invalidate();
        return true;
    }

If I press on the view and move my finger, the view follows it, now I would like to implement a swipe gesture, and it should work like this: If I swipe starting from the background and hit the view, I would like it to play a rotation animation based on which direction the swipe was (up, down, left or right). I still want to preserve the ability to move the views.

Imagine a coin, that I can move around, and if my finger flings it, it makes a 360° rotation

EDIT: Here's how my code for rotating the view works, but it works only if I start from the inside of the view that has to be rotated

public class OnSwipeTouchListener implements OnTouchListener {

    public final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

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

解决方案

You should override OnTouch() in your RelativeLayout and pass the MotionEvent on to a SimpleOnGestureListener. There you can return false for all the method except onFling() where you check whether the first event was outside the view you're going to rotate and the second event was inside the view you're going to rotate. If the events were not, you return false to indicate you didn't handle the event otherwise you rotate the coin and indicate that you handled the touch event by returning true.

As an extra point, I don't think you should be setting the right and bottom margins to -250 otherwise the view will extend to 250 pixels past the right and bottom edges of your RelativeLayout when I expect you want it to only extend to the intended width and height of the view respectively.

这篇关于做一个视图滑动式和活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆