1
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
if (imageView != null) {
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE :
ConstraintLayout.LayoutParams mParams = (ConstraintLayout.LayoutParams) imageView.getLayoutParams();
int x = (int) event.getRawX();
int y = (int) event.getRawY();
mParams.leftMargin = x - 50;
mParams.topMargin = y - 50;
imageView.setLayoutParams(mParams);
break;
default :
break;
}
return true;
}
});
}
此代碼的工作:選擇圖像和移動圖像在屏幕上,但我想移動屏幕上的觸摸 形象,我想在屏幕上觸摸移動圖像:如何使用移動包含在RelativeLayout的所有意見如何在屏幕上移動圖像觸摸?
請參閱Drop&Drag:https://developer.android.com/guide/topics/ui/drag-drop.html – Opiatefuchs