我正在制作一个游戏应用程序,我需要使用ondraglistener进行拖放,这是我在实现onDraglistener的类上所做的。
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.v("here","drag started");
//no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
Log.v("here","drag enteres");
break;
case DragEvent.ACTION_DRAG_LOCATION:
int mCurX = (int) event.getX();
int mCurY = (int) event.getY();
Log.v("Cur(X, Y) : " ,"here ::" + mCurX + ", " + mCurY );
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
Log.v("here","drag exits");
break;
case DragEvent.ACTION_DROP:
.................do what ever when dropped...
}
这里我在touchlistener上做的事情是拖延和放弃:
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/*
* Drag details: we only need default behavior
* - clip data could be set to pass data as part of drag
* - shadow can be tailored
*/
view.setTag("option"+index);
ClipData data = ClipData.newPlainText("tag", "option"+index);
shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
offsetX = (int)view.getX();//(int)motionEvent.getX();
offsetY = (int)view.getY();//motionEvent.getY();
Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());
return false;
}
现在它构建了阴影构建器,可以将其拖动到拖动手指的任何位置。现在,我怎样才能为阴影生成器设置动画,或者在未降落到目标时返回到其位置的视图。我尝试使用公共布尔值onInterceptTouchEvent(MotionEvent事件)并移动到父视图并获取x,y位置并使其动画化以返回其原始位置,但它会取消操作。有没有办法让x,y坐标到视图丢弃的位置而不是目标。然后我就能在位置之间制作动画。谢谢提前
答案 0 :(得分:-3)
你需要处理掉落:
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENTERED:
//break here was removed in purpose so you can see the shadow and the element is still at its initial place.
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
if (prioritiesList.contains(v)) {
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
break;
}