How to detect gesture over ImageView?
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);
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.
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);
}
}
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.
精彩评论