我想在android中使用滑动图像按钮就像android中的闹钟屏幕请帮我解决这个问题。 谢谢。screenshot of what I want to create
答案 0 :(得分:0)
您可以为它制作自定义ontouchlistener并相应地设置x和y位置。
以下是一个示例。
https://stackoverflow.com/a/19222290/5193608
来自它的MAIN代码部分(如果链接过期):
public class MultiTouchListener implements OnTouchListener
{
private float mPrevX;
private float mPrevY;
public MainActivity mainActivity;
public MultiTouchListener(MainActivity mainActivity1) {
mainActivity = mainActivity1;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
float currX,currY;
int action = event.getAction();
switch (action ) {
case MotionEvent.ACTION_DOWN: {
mPrevX = event.getX();
mPrevY = event.getY();
break;
}
case MotionEvent.ACTION_MOVE:
{
currX = event.getRawX();
currY = event.getRawY();
MarginLayoutParams marginParams = new MarginLayoutParams(view.getLayoutParams());
marginParams.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
view.setLayoutParams(layoutParams);
break;
}
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
现在在您的Main活动中,在您的视图上设置OnTouchListener ...这是您的imageButton或imageView;
MultiTouchListener touchListener=new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);
希望,这有帮助!。