Android 在手指触摸和弹出移动之间拖动弹出距离 公共类PopupDragutability实现了OnTouchListener{ ReaderLaunchActivity readerAct; PopupWindow targetWindow; 专用浮点数dX=0; 私有浮动dY=0; 公共PopupDraguity(ReaderLaunchActivity readerAct,PopupWindow targetWindow){ 超级(); this.readerAct=readerAct; this.targetWindow=targetWindow; } @凌驾 公共布尔onTouch(查看p_v、运动事件p_事件){ 开关(p_event.getAction()){ case MotionEvent.ACTION\u DOWN:{ dX=p_event.getRawX(); dY=p_event.getRawY(); 打破 } case MotionEvent.ACTION\u UP:{ 打破 } case MotionEvent.ACTION\u移动:{ if(readerAct.deviceOrientation==Configuration.ORIENTATION\u横向){ float newX=p_event.getRawX()-390; float newY=p_event.getRawY(); System.out.println(“newX”+newX); System.out.println(“newY”+newY); 如果(newX>-390&&newY>75&&(newX+targetWindow.getWidth())75&&(newX+targetWindow.getWidth())
我试图在屏幕边界内拖动一个矩形图标。 但是我面临一个问题,我的手指触摸之间有一段距离Android 在手指触摸和弹出移动之间拖动弹出距离 公共类PopupDragutability实现了OnTouchListener{ ReaderLaunchActivity readerAct; PopupWindow targetWindow; 专用浮点数dX=0; 私有浮动dY=0; 公共PopupDraguity(ReaderLaunchActivity readerAct,PopupWindow targetWindow){ 超级(); this.readerAct=readerAct; this.targetWindow=targetWindow; } @凌驾 公共布尔onTouch(查看p_v、运动事件p_事件){ 开关(p_event.getAction()){ case MotionEvent.ACTION\u DOWN:{ dX=p_event.getRawX(); dY=p_event.getRawY(); 打破 } case MotionEvent.ACTION\u UP:{ 打破 } case MotionEvent.ACTION\u移动:{ if(readerAct.deviceOrientation==Configuration.ORIENTATION\u横向){ float newX=p_event.getRawX()-390; float newY=p_event.getRawY(); System.out.println(“newX”+newX); System.out.println(“newY”+newY); 如果(newX>-390&&newY>75&&(newX+targetWindow.getWidth())75&&(newX+targetWindow.getWidth()),android,Android,我试图在屏幕边界内拖动一个矩形图标。 但是我面临一个问题,我的手指触摸之间有一段距离 和弹出式运动。我在帮助下暂时解决了这个问题 但谁能提出一个没有常数的解决方案呢。 此处目标窗口是弹出窗口的参考,读卡器布局是 整个屏幕的引用。您需要X和Y常量来保存偏移值。如果没有,则无法执行此操作,除非您同意窗口“捕捉”定位。但为什么有偏移值。为什么我的手指触摸图像时图像不移动。我认为在使用弹出窗口时。更新弹出窗口不会将x坐标作为图像的最左侧位置。我已为10英寸设备设置了常量。现在我希望代码为7英寸设备和5英
和弹出式运动。我在帮助下暂时解决了这个问题 但谁能提出一个没有常数的解决方案呢。 此处目标窗口是弹出窗口的参考,读卡器布局是
整个屏幕的引用。您需要X和Y常量来保存偏移值。如果没有,则无法执行此操作,除非您同意窗口“捕捉”定位。但为什么有偏移值。为什么我的手指触摸图像时图像不移动。我认为在使用弹出窗口时。更新弹出窗口不会将x坐标作为图像的最左侧位置。我已为10英寸设备设置了常量。现在我希望代码为7英寸设备和5英寸手机运行。使用常量不会帮个忙。谁能建议一个不使用常量的更好答案吗?“但我面临一个问题”这是什么问题?@pskink我的手指触摸位置和图像的移动之间有一段距离。因此,当我的手指触摸到屏幕的最左角时,图像和移动之间有一个偏移。它使用objectAnimator类,但我想使图像拖动。使用getX()而不是getRawX()时图像往往会模糊
public class PopupDragUtility implements OnTouchListener {
ReaderLaunchActivity readerAct;
PopupWindow targetWindow;
private float dX = 0;
private float dY = 0;
public PopupDragUtility(ReaderLaunchActivity readerAct, PopupWindow targetWindow) {
super();
this.readerAct = readerAct;
this.targetWindow = targetWindow;
}
@Override
public boolean onTouch(View p_v, MotionEvent p_event) {
switch (p_event.getAction()) {
case MotionEvent.ACTION_DOWN: {
dX = p_event.getRawX();
dY = p_event.getRawY();
break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_MOVE: {
if (readerAct.deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) {
float newX = p_event.getRawX() - 390;
float newY = p_event.getRawY();
System.out.println("newX " + newX);
System.out.println("newY " + newY);
if (newX > -390 && newY > 75 && (newX + targetWindow.getWidth()) < readerAct.readerLayout.getWidth()
&& (newY + targetWindow.getHeight()) < readerAct.readerLayout.getHeight()) {
targetWindow.update((int) newX, (int) newY, -1, -1, true);
}
} else {
float newX = p_event.getRawX() - 140;
float newY = p_event.getRawY();
System.out.println("newX " + newX);
System.out.println("newY " + newY);
if (newX > -140 && newY > 75 && (newX + targetWindow.getWidth()) < readerAct.readerLayout.getWidth()
&& (newY + targetWindow.getHeight()) < readerAct.readerLayout.getHeight()) {
targetWindow.update((int) newX, (int) newY, -1, -1, true);
}
}
break;
}
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
ObjectAnimator animation1, animation2;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
animation1 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
"y", touchY);
animation1.setDuration(2000);
animation1.start();
animation2 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
"x", touchX);
animation2.setDuration(2000);
animation2.start();
break;
case MotionEvent.ACTION_MOVE:
animation1 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
"y", touchY);
animation1.setDuration(2000);
animation1.start();
animation2 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
"x", touchX);
animation2.setDuration(2000);
animation2.start();
break;
case MotionEvent.ACTION_UP:
animation1 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
"y", touchY);
animation1.setDuration(2000);
animation1.start();
animation2 = ObjectAnimator.ofFloat(findViewById(R.id.imgLauncher),
"x", touchX);
animation2.setDuration(2000);
animation2.start();
break;
}
return false;
}