我有一张图像,我想在Long Press上移动并将图像放在拖动位置。有人告诉我用手势检测来做这件事,但我找不到合适的例子来理解它。 通过一个详细的例子帮助我理解这一点。 在此先感谢!!!
答案 0 :(得分:0)
所以,这是方法如何拖动视图
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}}
涉及onTouchListener
。另外我认为,你需要在onLongClickListener上设置View的状态(像布尔一样#34; isMoving")然后用onTouch移动它(if (isMoving)...
)
这是我在我的应用中的表现
float x;
float y;
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@SuppressWarnings("ResourceType")
@Override
public boolean onTouch(View view, MotionEvent event) {
Point point = (Point) view;
if (point.isEditind()) {
if (!point.isEditind()) return false;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) point.getLayoutParams();
//if (view.getId() != R.id.layout_counter) return false;
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
params.topMargin = (int) ((int) -y + event.getRawY() - point.getHeight() - f);
params.leftMargin = (int) ((int) -x + event.getRawX() - (point.getWidth() / 2));
point.setXY(params.leftMargin,params.topMargin);
params.rightMargin = -250;
params.bottomMargin = -250;
point.setLayoutParams(params);
point.notifyLines();
break;
case MotionEvent.ACTION_UP:
params.topMargin = (int) ((int) event.getRawY() - point.getHeight() - f);
params.leftMargin = (int) event.getRawX() - (point.getWidth() / 2);
point.setXY(params.leftMargin,params.topMargin);
params.rightMargin = -250;
params.bottomMargin = -250;
view.setLayoutParams(params);
point.notifyLines();
point.setEditing(false);
undoStack.push(((MoveAction)undoStack.pop()).setEndCords(params.leftMargin,params.topMargin));
break;
case MotionEvent.ACTION_DOWN:
x = event.getRawX() - point.getLeft();
y = event.getRawY() - point.getTop();
point.setXY(params.leftMargin,params.topMargin);
Log.d(TAG, "onTouch: xD:" + x + " yD:"+ y );
point.setLayoutParams(params);
point.notifyLines();
break;
}
return true;
}
else return false;
}
};
View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.d(TAG, "onLongClick: ");
Point point = (Point) view;
if (!point.isSelected())
{
point.setEditing(true);
undoStack.push(new MoveAction(MainActivity.this,point).setStartCords(point.getLeft(),(int)point.getTop()));
}
return true;
}
};