使用此link
我正在拖放一些图像,如果图像位置不正确,他们会移动到原始位置,但问题就出现了,但是,如果我在带有构建按钮的标签中运行我的应用程序(在标签内)屏幕)图像不仅来自按钮,而且还飞回来。但是这个问题并没有出现在buid标签中
我尝试的是将y
设置为-imagesize/2
,当它出现在
if(view.getY()>500){
view.setY(view.getY()-view.getHeight()/2)}
但我仍面临同样的问题,请指导我。 我的代码是
DROPAREA2.setOnDragListener(new MyDragListener(context));
ware.setOnClickListener(wareClick);
final class MyOnLongClickListener implements OnLongClickListener {
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("", "");
MyDragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
dragview = (RelativeLayout) view;
// view.setBackgroundColor(Color.GREEN);
// dragView = (TextView) view;
/*int[] location = new int[2];
dragview.getLocationOnScreen(location);
if(location[1]>520){
dragview.setY(location[1]-60);
// flyBackLayout(dragview);
Log.d("on shadow out of bound", ""+dragview.getY());
// dragview.setVisibility(INVISIBLE);
}*/
return true;
}
}
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
private static Drawable shadow;
View myView;
public MyDragShadowBuilder(View v) {
super(v);
myView = v;
// shadow = new ColorDrawable(Color.LTGRAY);
/* dragImgVw.setOnDragListener(new MyDragListener()); */
}
}
@Override
public boolean onDragEvent(DragEvent event) {
int mCurX = (int) event.getX();
int mCurY = (int) event.getY();
// int[] location = null;
if (event.getAction() == DragEvent.ACTION_DROP
|| event.getAction() == DragEvent.ACTION_DRAG_EXITED) {
Log.v("ONDRAGEVENThere", "ONDRAGEVENT it is :: " + mCurX + ", "
+ mCurY);
View view1 = (View) event.getLocalState();
view1.setVisibility(View.VISIBLE);
// /view1.getLocationOnScreen(location);
int[] location = new int[2];
int[] location1 = new int[2];
view1.getLocationOnScreen(location);
ObjectAnimator animationx = ObjectAnimator.ofFloat(view1,
"translationX", mCurX - view1.getWidth() / 2 - location[0],
0.0f);
ObjectAnimator animationy = ObjectAnimator.ofFloat(view1,
"translationY", mCurY - view1.getHeight() / 2 - location[1]
+ 10, 0.0f);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(500);
animSet.playTogether(animationx, animationy);
animSet.start();
}
return true;
}
public class MyDragListener implements OnDragListener {
MyDragListener(Context context) {
this.context = context;
}
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d("ACTION_DRAG_STARTED", "ACTION_DRAG_STARTED" + "");
// if(event.getY()>520)
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d("ACTION_DRAG_ENTERED", "ACTION_DRAG_ENTERED" + "");
/ break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d("ACTION_DRAG_LOCATION", "ACTION_DRAG_LOCATION" + "");
int mCurX = (int) event.getX();
int mCurY = (int) event.getY();
if (DEBUG)
Log.v("Cur(X, Y) : ", " CLASSr here ::" + mCurX + ", "
+ mCurY);
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d("ACTION_DRAG_EXITED", "ACTION_DRAG_EXITED" + "");
if (DEBUG)
Log.v("here", "drag exits");
/ /
break;
case DragEvent.ACTION_DROP:
Log.d("ACTION_DROP", "ACTION_DROP" + "");
// COUNTER=0;
// handle the dragged view being dropped over a drop view
RelativeLayout view = (RelativeLayout) event.getLocalState();
// stop displaying the view where it was before it was dragged
view.setVisibility(View.GONE);
// view dragged item is being dropped on
RelativeLayout dropTarget = (RelativeLayout) v;
// view being dragged and dropped
RelativeLayout dropped = (RelativeLayout) view;
dropped.setEnabled(false);
// /////////////////////DROP AREA 4
// dropTarget.setText(view.getText());
dropTarget.setGravity(Gravity.CENTER);
if (dropTarget.getId() == R.id.relDROPAREA2){
// //some animation
}
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d("ACTION_DRAG_ENDED", "ACTION_DRAG_ENDED" + "");
break;
default:
break;
}
return true;
}
}
OnClickListener wareClick = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// return false;
//some animation
}
};
}