当我按住ImageView
并将其拖动到屏幕上时,ImageView
变得不可见,并且ImageView
的可拖动版本将以屏幕的中心出现在屏幕上指尖下方的生成的视图;这不是有机的。我希望视图的可拖动版本直接在ImageView
上方产生,而不是直接在指尖下方产生。
例如,如果我的ImageView
是卡片组顶部的钻石王牌,并且我想将卡片拖离卡片组,则卡片应平稳地拖离卡片组,而不要将其自身重新放置在下面我的手指,然后拖动(如果有道理的话)。
这是我的可拖动ImageView代码;我不确定如何使拖动动画如上所述更平滑。
public class DragView2 extends AppCompatImageView
implements View.OnDragListener, View.OnTouchListener{
private static final String TAG = DragView2.class.getSimpleName();
public DragView2(Context context)
{
super(context, null);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
public DragView2(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case ACTION_DOWN:
Log.d(TAG, "ACTION_DOWN");
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
this.setVisibility(View.INVISIBLE);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
return false;
}
@Override
public boolean onDrag(View view, DragEvent event)
{
final View viewToDrag = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG,"ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG,"ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG,"ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DROP:
Log.d(TAG,"ACTION_DROP");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG,"ACTION_DRAG_ENDED");
view.post(new Runnable() {
public void run() {
viewToDrag.setVisibility(View.VISIBLE);
}
});
break;
default:
Log.d(TAG,"default");
break;
}
return true;
}
}
答案 0 :(得分:0)
指向解决方案的指针。
我扩展了 View 类。使用以下代码成功将对象拖动到正确的坐标。
@Override
public boolean onTouchEvent(MotionEvent event)
{
float oldX, oldY, newX, newY, originalX, originalY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
originalX = oldX;
originalY = oldY;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
updatePositions(oldX, oldY, newX, newY);
oldX = newX;
oldY = newY;
invalidate();
break;
case MotionEvent.ACTION_UP:
newX = event.getX();
newY = event.getY();
// USE ANY OF THE FOLLOWING
// updatePositions(oldX, oldY, newX, newY);
// updatePositions(originalX, originalY, newX, newY);
oldX = newX;
oldY = newY;
}
}
private void updatePositions(float pt1, float pt2, float pt3, float pt4) {
// code to change the position of the position
}
制作6个变量类变量,并使用Toast显示位置。希望这能提示解决方案:)