Я пытаюсь создать приложение, которое может перемещать ImageView
на вашем устройстве, например, перетаскивание, и когда я помещаю, например, 75% ImageView
на экран показывает Toast
. Я читал о MotionEvent
и onTouchListener
, и я следил за этим question, но это меня не убеждает.
Изменить
Мой текущий код:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
int windowwidth;
int windowheight;
private ImageView mImageView;
private ViewGroup mRrootLayout;
private int _xDelta;
private int _yDelta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
windowwidth = displaymetrics.widthPixels;
windowheight = displaymetrics.heightPixels;
mRrootLayout = (ViewGroup) findViewById(R.id.root);
mImageView = (ImageView) mRrootLayout.findViewById(R.id.im_move_zoom_rotate);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
mImageView.setLayoutParams(layoutParams);
mImageView.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
if(X == 0){
Toast.makeText(this, "OUT", Toast.LENGTH_SHORT).show();
}
else if (Y == 0){
Toast.makeText(this, "OUT", Toast.LENGTH_SHORT).show();
}
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;
}
mRrootLayout.invalidate();
return true;
}
}
Я сделал те if
и else if
только для того, чтобы узнать, выходит ли ImageView
из устройства, на левой и правой стороне устройства, похоже, все в порядке, но я хотел бы сделать это чище и не hardwritted, также я не получаю LayoutParams(150,150)
почему 150? Также я не понимаю, почему мне нужно создать RelativeLayout.LayoutParams
и почему я должен поставить
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
Я сделал if/else if
, потому что хочу удалить, когда пользователь хочет вывести ImageView
из устройства, поэтому мне нужно контролировать, когда он пытается, на данный момент я получил только TOP/LEFT/ПРАВИЛЬНО не вниз, я также получаю размеры моего устройства, чтобы попытаться, если X или Y совпадают с высотой или widht, просто покажите Toast
, но он не делает это правильно.
Теперь мой ImageView
- это ic_launcher
, но он будет больше (почти средний экран).
Примечание
Если вы знаете какой-либо другой способ сделать это проще или чище, не стесняйтесь его размещать здесь, меня не интересует мой код, я могу его адаптировать, я просто хочу, чтобы он был ясным и не был жестко закодирован.