我有一个包含按钮的relativeLayout。我需要根据用户交互的移动仅按水平方式移动按钮,并且只在起始点的左侧(如whatsapp屏幕音频按钮)
mButton = (ImageButton) findViewById(R.id.button);
mPrevX,mPrevY =>起始坐标
mPrevX = mButton.getX();
mPrevY = mButton.getY();
currX,currY =>当前坐标
currX = event.getRawX();
currY = event.getRawY();
移动按钮的代码
mButton = (ImageButton) findViewById(R.id.button);
ViewGroup.MarginLayoutParams marginParams =
new ViewGroup.MarginLayoutParams(v.getLayoutParams());
marginParams.setMargins((int)(currX-200), (int)(mlayout.getWidth()-mPrevY )
,(int)(mlayout.getWidth()-currX),(int)(mlayout.getHeight()));
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(marginParams);
mButton.setLayoutParams(layoutParams);
问题是它正在移动但是除了屏幕上的用户交互之外的方向帮助我这样做。