我创建了一个简单的录像机应用程序(典型的cameraapi和媒体录像机),它有一个可拖动的浮动视图,在我的例子中是一个按钮。
问题是,当它没有录制时,浮动按钮会以正确的方式跟随我的手指,但是如果我开始录制并尝试拖动按钮,视图会在我拖动它之前更改位置。就好像按钮从我的手指上跑开了,或者我的手指按下了触摸屏上的视图。
我的ontouchlistener类来自:https://stackoverflow.com/a/56920148/13196984 但只是用java转换的。我也试过这个,但还是一样的问题。https://stackoverflow.com/a/57957226/13196984
public class CustomTouchHelper implements View.OnTouchListener{
private int screenWidth;
private int screenHeight;
private float dX = 0f;
private float dY = 0f;
public CustomTouchHelper(int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float newX;
float newY;
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
initialX = view.getX();
initialY = view.getY();
dX = view.getX() - motionEvent.getRawX();
dY = view.getY() - motionEvent.getRawY();
System.out.println(" DOWN X " + view.getX());
System.out.println("DOWN Y " + view.getY());
break;
case MotionEvent.ACTION_MOVE:
newX = motionEvent.getRawX() + dX;
newY = motionEvent.getRawY() + dY;
System.out.println(" MOVED X " + view.getX());
System.out.println("MOVED Y " + view.getY());
if((newX <= 0) || newX >= screenWidth - view.getWidth()
|| (newY <= 0 ||
newY >= screenHeight
- view.getHeight())){
return true;
}
view.animate().x(newX).y(newY).setDuration(0).start();
break;
case ACTION_UP:
System.out.println(" UP X " + view.getX());
System.out.println("UP Y " + view.getY());
if((Math.abs(view.getX() - initialX) <=10)&& (Math.abs(view.getY() - initialY) <=10)){
view.performClick();
}
}
return true;
}
}
我试着打印出touchlistener上的什么动作改变了视图位置,下面是我得到的
不录制时只需单击浮动按钮
2020-11-20 20:43:35.806 20552-20552/com.xxxx .xxxx I/System.out: DOWN X 456.57724
2020-11-20 20:43:35.806 20552-20552/com.xxxx .xxxx I/System.out: DOWN Y 845.5596
2020-11-20 20:43:35.825 20552-20552/com.xxxx .xxxx I/System.out: MOVED X 456.57724
2020-11-20 20:43:35.825 20552-20552/com.xxxx .xxxx I/System.out: MOVED Y 845.5596
2020-11-20 20:43:35.842 20552-20552/com.xxxx .xxxx I/System.out: MOVED X 456.57724
2020-11-20 20:43:35.843 20552-20552/com.xxxx .xxxx I/System.out: MOVED Y 845.5596
2020-11-20 20:43:35.859 20552-20552/com.xxxx .xxxx I/System.out: MOVED X 456.57724
2020-11-20 20:43:35.859 20552-20552/com.xxxx .xxxx I/System.out: MOVED Y 845.5596
2020-11-20 20:43:35.876 20552-20552/com.xxxx .xxxx I/System.out: MOVED X 456.57724
2020-11-20 20:43:35.876 20552-20552/com.xxxx .xxxx I/System.out: MOVED Y 845.5596
2020-11-20 20:43:35.893 20552-20552/com.xxxx .xxxx I/System.out: MOVED X 456.57724
2020-11-20 20:43:35.893 20552-20552/com.xxxx .xxxx I/System.out: MOVED Y 845.5596
2020-11-20 20:43:35.905 20552-20552/com.xxxx .xxxx I/System.out: MOVED X 456.57724
2020-11-20 20:43:35.905 20552-20552/com.xxxx .xxxx I/System.out: MOVED Y 845.5596
2020-11-20 20:43:35.907 20552-20552/com.xxxx .xxxx I/System.out: UP X 456.57724
2020-11-20 20:43:35.907 20552-20552/com.xxxx .xxxx I/System.out: UP Y 845.5596
在录制过程中单击一个简单的按钮
2020-11-20 20:46:24.113 20552-20552/com.xxxx.xxxxI/System.out: DOWN X 456.57724
2020-11-20 20:46:24.113 20552-20552/com.xxxx.xxxxI/System.out: DOWN Y 845.5596
2020-11-20 20:46:24.115 20552-20552/com.xxxx.xxxxI/System.out: MOVED X 456.57724
2020-11-20 20:46:24.115 20552-20552/com.xxxx.xxxxI/System.out: MOVED Y 845.5596
2020-11-20 20:46:24.126 20552-20552/com.xxxx.xxxxI/System.out: MOVED X 456.57724
2020-11-20 20:46:24.127 20552-20552/com.xxxx.xxxxI/System.out: MOVED Y 845.5596
2020-11-20 20:46:24.142 20552-20552/com.xxxx.xxxxI/System.out: MOVED X 545.49493***X Changed
2020-11-20 20:46:24.142 20552-20552/com.xxxx.xxxxI/System.out: MOVED Y 921.52 ***Y Changed
2020-11-20 20:46:24.155 20552-20552/com.xxxx.xxxxI/System.out: MOVED X 545.49493
2020-11-20 20:46:24.155 20552-20552/com.xxxx.xxxxI/System.out: MOVED Y 921.52
2020-11-20 20:46:24.157 20552-20552/com.xxxx.xxxxI/System.out: UP X 545.49493
2020-11-20 20:46:24.157 20552-20552/com.xxxx.xxxxI/System.out: UP Y 921.52
希望你能帮我。:)
暂无答案!
目前还没有任何答案,快来回答吧!