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;
}我正在尝试在屏幕边界内拖动一个矩形图标。但是我面临着一个问题,那就是我的手指接触
and the pop up movement.I have temporary resolved this issue with the help of some constants.But Can anyone suggest a solution without constants. Here target window is the reference of the popup and reader layout is the reference of the entire screen.发布于 2015-04-16 13:01:38
@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;
}https://stackoverflow.com/questions/29664913
复制相似问题