我有一个带有图像视图的图库。我在画廊上设置了一个ontouch事件,我可以从图库中拖动图像并将其放在其他地方。我可以拖动它,但正如我在onTouch事件中所做的那样,我的画廊不再向上滚动。有什么能够区分onTouch和滑动动作吗? 这是我的画廊的OnTouchEvent
final GestureDetector gdt = new GestureDetector(new GestureListener());
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
private static final int SWIPE_MAX_OFF_PATH = 100;
gallery.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg1, MotionEvent event) {
if(gdt.onTouchEvent(event)){
Log.v(TAG,"gesture detector");
return true;
}
if(event.getAction()==MotionEvent.ACTION_DOWN){
Gallery parent =(Gallery) arg1;
int x=(int)event.getX();
int y=(int) event.getY();
int position=parent.pointToPosition(x, y);
ImageView viewDraggedFromGallery=(ImageView)parent.getChildAt(position);
if(viewDraggedFromGallery.getTag() != null){
ClipData.Item item=new ClipData.Item((CharSequence)viewDraggedFromGallery.getTag().toString());
ClipDescription NOTE_STREAM_TYPES= new ClipDescription ((CharSequence)viewDraggedFromGallery.getTag().toString(),new String[ ] {ClipDescription.MIMETYPE_TEXT_PLAIN});
ClipData clipData=new ClipData(NOTE_STREAM_TYPES,item);
clipData.addItem(item);
DragShadowBuilder shadowBuilder=new View.DragShadowBuilder(viewDraggedFromGallery);
arg1.startDrag(clipData, shadowBuilder, viewDraggedFromGallery, 0);
return true;
}
else{
Log.v(TAG,"gallery on touch- Invalid Tag");
return false;
}
}
return false;
}
public boolean dispatchTouchEvent(MotionEvent ev){
Log.v(TAG,"touch event dispatched");
super.dispatchTouchEvent(ev);
Log.v(TAG,"gdt.OnTouchEvent(ev) " + gdt.onTouchEvent(ev) );
return gdt.onTouchEvent(ev);
}
private class GestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.v(TAG,"motion event e1 = " + e1);
Log.v(TAG,"motion event e2 = " + e2);
float dX = e2.getX()-e1.getX();
float dY = e1.getY()-e2.getY();
if (Math.abs(dY)<SWIPE_MAX_OFF_PATH &&
Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
return true;
}
return false;
}
}
答案 0 :(得分:0)
您应该自己构建一个自定义手势检测器,它可以同时处理onTouch()
和onFling()
(“fling”是Android中“轻扫”的同义词)。
看看这个tutorial