How to detect gesture over ImageView?

…衆ロ難τιáo~ 提交于 2020-02-21 06:44:05

问题


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

回答1:


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

     }

}



回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/6492580/how-to-detect-gesture-over-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!