添加一扔手势的图像视图 - 机器人 [英] Adding Fling Gesture to an image view - Android

查看:77
本文介绍了添加一扔手势的图像视图 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在引用code在这里:机器人 - 基本手势检测

但就是无法得到它的工作。在我的主要活动我有一个简单的图像定义。我要检测的图像上一扔。下面是我下面的code。在底部的onclick方法为空。就是因为这个原因吗?我把它空,因为在其他的code样品的不是我想要的。我只想要一个简单的举杯弹出说一扔向右或向左猛冲

 公共类GestureRightLeft扩展活动实现OnClickListener {

    ImageView的窥视;

    私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
    私有静态最终诠释SWIPE_MAX_OFF_PATH = 250;
    私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
    私人GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        偷看=(ImageView的)findViewById(R.id.peek);
        peek.setImageResource(R.drawable.bluestrip);

        gestureDetector =新GestureDetector(新MyGestureDetector());
        gestureListener =新View.OnTouchListener(){
            公共布尔onTouch(视图V,MotionEvent事件){
                如果(gestureDetector.onTouchEvent(事件)){
                    返回true;
                }
                返回false;
            }
        };
    }

    类MyGestureDetector扩展SimpleOnGestureListener {
        @覆盖
        公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
            尝试 {
                如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)
                    返回false;
                //从右向左轻扫
                如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                    Toast.makeText(GestureRightLeft.this,左刷卡,Toast.LENGTH_SHORT).show();
                }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                    Toast.makeText(GestureRightLeft.this,右刷卡,Toast.LENGTH_SHORT).show();
                }
            }赶上(例外五){
                //什么
            }
            返回false;
        }
    }

    @覆盖
    公共无效的onClick(视图v){}
}
 

解决方案

这里的simpliest工作版本护圈,我能想到的。 实际上,你可以它绑在任何部件,不仅ImageView的。

 公共类MyActivity延伸活动{
    私人无效的onCreate(){
        最后GestureDetector GDT =新GestureDetector(新GestureListener());
        最后的ImageView ImageView的=(ImageView的)findViewById(R.id.image_view);
        imageView.setOnTouchListener(新OnTouchListener(){
            @覆盖
            公共布尔onTouch(最终视图中查看,最终MotionEvent事件){
                gdt.onTouchEvent(事件);
                返回true;
            }
        });
    }

    私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
    私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;

    私有类GestureListener扩展SimpleOnGestureListener {
        @覆盖
        公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
            如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                返回false; //从右到左
            }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                返回false; // 从左到右
            }

            如果(e1.getY() -  e2.getY()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityY)> SWIPE_THRESHOLD_VELOCITY){
                返回false; //从下到上
            }否则,如果(e2.getY() -  e1.getY()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityY)> SWIPE_THRESHOLD_VELOCITY){
                返回false; // 从上到下
            }
            返回false;
        }
    }
}
 

没有活动onClickListener但(如果你不需要捕获任何的onclick行动) 它捕捉不仅水平,但纵也(只是删除垂直部分,如果你不需要它),和水平刷卡有优先权,你可以看到。 在方法返回(NAD在我的意见是)只是调用你的方法或任何地方:)

Okay I have been referencing the code here: Android - basic gesture detection

but just can not get it to work. In my main activity I have a simple image defined. I want to detect a fling on the image. Here is my code below. The onclick method at the bottom is empty. Is it because of this? I left it blank because in the other code sample its not what I want. I just want a simple toast to pop up saying fling right or fling left.

public class GestureRightLeft extends Activity implements OnClickListener  {

    ImageView peek;

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        peek =(ImageView) findViewById(R.id.peek);
        peek.setImageResource(R.drawable.bluestrip);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(GestureRightLeft.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(GestureRightLeft.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public void onClick(View v) {}
}

解决方案

Here's the simpliest working version of flinger I can think of. You can actually tie it to any component, not only ImageView.

public class MyActivity extends Activity {
    private void onCreate() {
        final GestureDetector gdt = new GestureDetector(new GestureListener());
        final ImageView imageView  = (ImageView) findViewById(R.id.image_view);
        imageView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                gdt.onTouchEvent(event);
                return true;
            }
        });
    }               

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private class GestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Right to left
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Left to right
            }

            if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Bottom to top
            }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Top to bottom
            }
            return false;
        }
    }
}

No activity onClickListener though (if you don't need to catch any onclick actions) It captures not only horizontal, but vertical also (just delete vertical part if you don't need it), and horizontal swipes have priority as you can see. In places where method returns (nad where my comments are) just call your method or whatever :)

这篇关于添加一扔手势的图像视图 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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