How to detect gesture over ImageView?

。_饼干妹妹 提交于 2019-12-04 20:19:58

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

     }

}

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.

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.

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.

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