0

我有一个带有 imageviews 的画廊。我在图库上设置了一个 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;
    }
}
4

1 回答 1

0

您应该为自己构建一个自定义手势检测器,它可以同时处理onTouch()onFling()(“fling”是 Android 中“swipe”的同义词)。

看看这个教程

于 2013-04-25T23:40:29.217 回答