我有一個自定義相對佈局,我可以拖放一些 按鈕。我開始拖動我的onTouch:刪除或取消DragShadow
/**
*
*/
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN : {
ClipData data = ClipData.newPlainText("", "");
ImageButton imageButton = (ImageButton)view;
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(imageButton);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
} break;
case MotionEvent.ACTION_UP: {
view.performClick();
view.setVisibility(View.VISIBLE);
} break;
}
return true;
}
正如你所看到的我創建一個DragShadow。我現在有一個小問題,因爲我定義了一個拖拽區域。當用戶拖動按鈕進行拖動區域的拖動被取消:
/**
*
*/
@Override
public boolean onDrag(View view, DragEvent event) {
View sourceView = (View) event.getLocalState();
float sourceX = sourceView.getX();
float sourceY = sourceView.getY();
float dropX = event.getX() - (sourceView.getWidth()/2);
float dropY = event.getY() - (sourceView.getHeight()/2);
switch(event.getAction()) {
case DragEvent.ACTION_DRAG_EXITED : {
TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0);
animation.setDuration(300);
sourceView.startAnimation(animation);
sourceView.setX(sourceX);
sourceView.setY(sourceY);
sourceView.setVisibility(View.VISIBLE);
} break;
case DragEvent.ACTION_DROP : {
sourceView.setX(dropX);
sourceView.setY(dropY);
sourceView.setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0);
animation.setDuration(300);
sourceView.startAnimation(animation);
sourceView.setX(sourceX);
sourceView.setY(sourceY);
} break;
}
return true;
}
}
因此,當用戶離開該阻力區域的按鈕被使用動畫自動移回原來的位置。問題是DragShadow仍然可見,我找不到方法來刪除或取消DragShadow。只要用戶觸摸屏幕,DragShadow就可見。
那麼,如何在ACTION_DRAG_EXITED被觸發時以編程方式取消或刪除DragShadow?