如何检测 ImageView 上的手势?

发布于 2024-11-17 03:05:24 字数 2248 浏览 3 评论 0原文

我想检测手指被拖动到 ImageView 上。我获取 ImageView,创建 GestureDetector 和 View.OnTouchListener 的实例,然后将 View.OnTouchListener 设置为 ImageView。

OnTouchListener 检测所有触摸(“发生触摸”),并将它们传递给 GestureDetector,但它从不调用其 onScroll 或 onFling 方法。我做错了什么?下面你可以看到相关来源:

    myImageView = (ImageView) findViewById(R.id.imageView1);

    myGestureDetector = new GestureDetector(this,
            new GestureDetector.OnGestureListener() {

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void onShowPress(MotionEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
                    // TODO Auto-generated method stub
                    Log.d("gestures", "scroll");
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2,
                        float velocityX, float velocityY) {
                    // TODO Auto-generated method stub
                    Log.d("gestures", "fling");
                    return true;
                }

                @Override
                public boolean onDown(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }

            });



    gestureListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (myGestureDetector.onTouchEvent(event)) {
                Log.d("gestures", "here");
                return true;
            }

            Log.d("gestures", "touch occured");
            return false;
        }
    };

    myImageView.setOnTouchListener(gestureListener);

I'd like to detect finger being dragged over ImageView. I get my ImageView, create instances of GestureDetector and View.OnTouchListener, then set View.OnTouchListener to ImageView.

OnTouchListener detects all touches ("touch occured") and I pass them to GestureDetector, but it never calls its methods onScroll or onFling. What I am doing wrong? Bellow you can see related source:

    myImageView = (ImageView) findViewById(R.id.imageView1);

    myGestureDetector = new GestureDetector(this,
            new GestureDetector.OnGestureListener() {

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void onShowPress(MotionEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
                    // TODO Auto-generated method stub
                    Log.d("gestures", "scroll");
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2,
                        float velocityX, float velocityY) {
                    // TODO Auto-generated method stub
                    Log.d("gestures", "fling");
                    return true;
                }

                @Override
                public boolean onDown(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }

            });



    gestureListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (myGestureDetector.onTouchEvent(event)) {
                Log.d("gestures", "here");
                return true;
            }

            Log.d("gestures", "touch occured");
            return false;
        }
    };

    myImageView.setOnTouchListener(gestureListener);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耀眼的星火 2024-11-24 03:05:24

OnGestureListener 的 onDown 方法必须始终返回 true,否则检测器不会对以下事件做出反应,从而不会调用 onScroll。

onDown method of OnGestureListener must always return true otherwise the detector won't react to the following events as a result onScroll won't be invoked.

烙印 2024-11-24 03:05:24

我认为您还需要重写 onTouchEvent 方法。我在 imageview 上检测到了 onFling() 手势,如下所示。

   @Override
    public boolean onTouchEvent(MotionEvent event){
                return myGestureDetector.onTouchEvent(event);
    }

在我这边工作的代码是:

 public class AddNewTask extends Activity implements OnClickListener,OnTouchListener ,GestureDetector.OnGestureListener
    {
  private GestureDetector gestures;
    private ImageView imgViewPreview;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addtask);

     gestures = new GestureDetector(AddNewTask.this,
                         this);
    imgViewPreview.setOnTouchListener(this);

    }

    public boolean onTouch(View v, MotionEvent event)
     {
        if(v==imgViewPreview)
          if(gestures.onTouchEvent(event))
             return true;


        return false;
    }

    @Override
     public boolean onTouchEvent(MotionEvent event) 
    {
       return gestures.onTouchEvent(event);      

     }

}

I think you will also need to override the onTouchEvent method. I have detected onFling() gesture on imageview like this.

   @Override
    public boolean onTouchEvent(MotionEvent event){
                return myGestureDetector.onTouchEvent(event);
    }

The code which is working at my end is:

 public class AddNewTask extends Activity implements OnClickListener,OnTouchListener ,GestureDetector.OnGestureListener
    {
  private GestureDetector gestures;
    private ImageView imgViewPreview;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addtask);

     gestures = new GestureDetector(AddNewTask.this,
                         this);
    imgViewPreview.setOnTouchListener(this);

    }

    public boolean onTouch(View v, MotionEvent event)
     {
        if(v==imgViewPreview)
          if(gestures.onTouchEvent(event))
             return true;


        return false;
    }

    @Override
     public boolean onTouchEvent(MotionEvent event) 
    {
       return gestures.onTouchEvent(event);      

     }

}
征﹌骨岁月お 2024-11-24 03:05:24

尝试通过扩展 SimpleOnGestureListener 来使用自定义手势侦听器类。另外,通过这样做,您也将能够覆盖上述所有方法。

Try using a customised gesture listener class by extending SimpleOnGestureListener. Also by doing so, you will be able to override all the above methods as well.

或十年 2024-11-24 03:05:24

首先解决您在使用@Override时遇到的错误。

这主要是因为Java编译器配置较低[应用程序属性->Java编译器->编译器合规级别]确保它是1.6。

解决此问题后,请使用 @Override 符号重试。

First resolve the error which you are facing when you have used @Override.

This is mostly because of the lower Java compiler Configurations[application properties->Java Compiler->Compiler compliance level] Make sure it is 1.6.

Once this is solved, try again with @Override notations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文