0
我只想在Y方向上拖放一個按鈕。我實現了這個功能。但它跨越屏幕邊界並消失。請幫助我實現我的目標。拖放一個按鈕?
這裏是我的代碼
public class ChoiceTouchListener implements View.OnTouchListener{
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int x = (int) motionEvent.getRawX();
int y = (int) motionEvent.getRawY();
DisplayMetrics metrics = getResources().getDisplayMetrics();
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams tparams = (RelativeLayout.LayoutParams) view.getLayoutParams();
changey = y- tparams.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 params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.topMargin = y-changey;
view.setLayoutParams(params);
break;
}
rootlayout.invalidate();
return true;
}
}
什麼是你的代碼中的「v」? – Hasnain
查看onTouch的參數。您可以在onTouch中編寫代碼並通過查看 – Anmol
來代替v。讓我試試這個代碼。 – Hasnain