我正在尝试在FrameLayout中制作可移动文本。在TextView中,我设置了OnTouchListener,您可以在下面看到它。问题是,当我触摸textview(不动手指)时,OnTouchListener给出了不同的位置,而我的TextView则在不同的位置之间跳跃。.我也输入了LogCat结果,这与这种情况有关。感谢您的任何帮助。
onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.w("KKK", "ONTOUCH EVENT: "+motionEvent.getAction());
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lastTouchX = motionEvent.getX();
lastTouchY = motionEvent.getY();
Log.w("KKK", "ACTION DOWN X: " + lastTouchX + " Y: " + lastTouchY);
break;
case MotionEvent.ACTION_MOVE:
Log.w("KKK", "ACTION MOVE BEFORE X: " + lastTouchX + " Y: " + lastTouchY);
float beforeX = lastTouchX, beforeY = lastTouchY;
lastTouchX = motionEvent.getX();
lastTouchY = motionEvent.getY();
Log.w("KKK", "ACTION MOVE AFTER X: " + lastTouchX + " Y: " + lastTouchY);
Log.w("KKK", "ACTION MOVE DELTA X: " + (beforeX - lastTouchX) + " Y: " + (beforeY - lastTouchY));
float delX = lastTouchX - beforeX;
float delY = lastTouchY - beforeY;
ViewGroup.MarginLayoutParams lp1 = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp1.setMargins(lp1.leftMargin+(int)delX, lp1.topMargin+(int)delY, lp1.rightMargin, lp1.bottomMargin);
view.setLayoutParams(lp1);
break;
}
return true;
};
};
日志猫:
07-23 14:02:49.494: W/KKK(24257): ONTOUCH EVENT: 0
07-23 14:02:49.494: W/KKK(24257): ACTION DOWN X: 80.0 Y: 70.0
07-23 14:02:49.514: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.514: W/KKK(24257): ACTION MOVE BEFORE X: 80.0 Y: 70.0
07-23 14:02:49.514: W/KKK(24257): ACTION MOVE AFTER X: 185.4158 Y: 73.4769
07-23 14:02:49.514: W/KKK(24257): ACTION MOVE DELTA X: -105.4158 Y: -3.4768982
07-23 14:02:49.554: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.554: W/KKK(24257): ACTION MOVE BEFORE X: 185.4158 Y: 73.4769
07-23 14:02:49.554: W/KKK(24257): ACTION MOVE AFTER X: 71.92239 Y: 68.4794
07-23 14:02:49.554: W/KKK(24257): ACTION MOVE DELTA X: 113.49341 Y: 4.9974976
07-23 14:02:49.574: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.574: W/KKK(24257): ACTION MOVE BEFORE X: 71.92239 Y: 68.4794
07-23 14:02:49.574: W/KKK(24257): ACTION MOVE AFTER X: 185.43243 Y: 72.4794
07-23 14:02:49.574: W/KKK(24257): ACTION MOVE DELTA X: -113.51004 Y: -4.0
07-23 14:02:49.604: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.604: W/KKK(24257): ACTION MOVE BEFORE X: 185.43243 Y: 72.4794
07-23 14:02:49.604: W/KKK(24257): ACTION MOVE AFTER X: 68.70996 Y: 68.4794
07-23 14:02:49.604: W/KKK(24257): ACTION MOVE DELTA X: 116.72247 Y: 4.0
07-23 14:02:49.644: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.644: W/KKK(24257): ACTION MOVE BEFORE X: 68.70996 Y: 68.4794
07-23 14:02:49.644: W/KKK(24257): ACTION MOVE AFTER X: 183.23611 Y: 71.7446
07-23 14:02:49.644: W/KKK(24257): ACTION MOVE DELTA X: -114.52615 Y: -3.2651978
07-23 14:02:49.654: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.654: W/KKK(24257): ACTION MOVE BEFORE X: 183.23611 Y: 71.7446
07-23 14:02:49.654: W/KKK(24257): ACTION MOVE AFTER X: 67.1391 Y: 68.04501
07-23 14:02:49.654: W/KKK(24257): ACTION MOVE DELTA X: 116.097015 Y: 3.699585
07-23 14:02:49.694: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.694: W/KKK(24257): ACTION MOVE BEFORE X: 67.1391 Y: 68.04501
07-23 14:02:49.694: W/KKK(24257): ACTION MOVE AFTER X: 184.44492 Y: 71.48065
07-23 14:02:49.694: W/KKK(24257): ACTION MOVE DELTA X: -117.30582 Y: -3.4356384
07-23 14:02:49.714: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.714: W/KKK(24257): ACTION MOVE BEFORE X: 184.44492 Y: 71.48065
07-23 14:02:49.714: W/KKK(24257): ACTION MOVE AFTER X: 67.444916 Y: 68.48065
07-23 14:02:49.714: W/KKK(24257): ACTION MOVE DELTA X: 117.0 Y: 3.0
07-23 14:02:49.724: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.724: W/KKK(24257): ACTION MOVE BEFORE X: 67.444916 Y: 68.48065
07-23 14:02:49.724: W/KKK(24257): ACTION MOVE AFTER X: 184.44492 Y: 71.48065
07-23 14:02:49.724: W/KKK(24257): ACTION MOVE DELTA X: -117.0 Y: -3.0
07-23 14:02:49.764: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.764: W/KKK(24257): ACTION MOVE BEFORE X: 184.44492 Y: 71.48065
07-23 14:02:49.764: W/KKK(24257): ACTION MOVE AFTER X: 67.444916 Y: 68.48065
07-23 14:02:49.764: W/KKK(24257): ACTION MOVE DELTA X: 117.0 Y: 3.0
07-23 14:02:49.794: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.794: W/KKK(24257): ACTION MOVE BEFORE X: 67.444916 Y: 68.48065
07-23 14:02:49.794: W/KKK(24257): ACTION MOVE AFTER X: 179.32318 Y: 70.19916
07-23 14:02:49.794: W/KKK(24257): ACTION MOVE DELTA X: -111.878265 Y: -1.7185059
07-23 14:02:49.814: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.814: W/KKK(24257): ACTION MOVE BEFORE X: 179.32318 Y: 70.19916
07-23 14:02:49.814: W/KKK(24257): ACTION MOVE AFTER X: 69.45322 Y: 69.4819
07-23 14:02:49.814: W/KKK(24257): ACTION MOVE DELTA X: 109.869965 Y: 0.71725464
07-23 14:02:49.854: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.854: W/KKK(24257): ACTION MOVE BEFORE X: 69.45322 Y: 69.4819
07-23 14:02:49.854: W/KKK(24257): ACTION MOVE AFTER X: 173.34717 Y: 69.4819
07-23 14:02:49.854: W/KKK(24257): ACTION MOVE DELTA X: -103.89395 Y: 0.0
07-23 14:02:49.864: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.864: W/KKK(24257): ACTION MOVE BEFORE X: 173.34717 Y: 69.4819
07-23 14:02:49.864: W/KKK(24257): ACTION MOVE AFTER X: 71.46155 Y: 69.4819
07-23 14:02:49.864: W/KKK(24257): ACTION MOVE DELTA X: 101.88562 Y: 0.0
07-23 14:02:49.904: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.904: W/KKK(24257): ACTION MOVE BEFORE X: 71.46155 Y: 69.4819
07-23 14:02:49.904: W/KKK(24257): ACTION MOVE AFTER X: 172.46155 Y: 69.4819
07-23 14:02:49.904: W/KKK(24257): ACTION MOVE DELTA X: -101.0 Y: 0.0
07-23 14:02:49.934: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.934: W/KKK(24257): ACTION MOVE BEFORE X: 172.46155 Y: 69.4819
07-23 14:02:49.934: W/KKK(24257): ACTION MOVE AFTER X: 71.46155 Y: 69.4819
07-23 14:02:49.934: W/KKK(24257): ACTION MOVE DELTA X: 101.0 Y: 0.0
07-23 14:02:49.975: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:49.975: W/KKK(24257): ACTION MOVE BEFORE X: 71.46155 Y: 69.4819
07-23 14:02:49.975: W/KKK(24257): ACTION MOVE AFTER X: 172.46155 Y: 69.4819
07-23 14:02:49.975: W/KKK(24257): ACTION MOVE DELTA X: -101.0 Y: 0.0
07-23 14:02:50.015: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.015: W/KKK(24257): ACTION MOVE BEFORE X: 172.46155 Y: 69.4819
07-23 14:02:50.015: W/KKK(24257): ACTION MOVE AFTER X: 71.46155 Y: 69.4819
07-23 14:02:50.015: W/KKK(24257): ACTION MOVE DELTA X: 101.0 Y: 0.0
07-23 14:02:50.025: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.025: W/KKK(24257): ACTION MOVE BEFORE X: 71.46155 Y: 69.4819
07-23 14:02:50.035: W/KKK(24257): ACTION MOVE AFTER X: 172.46155 Y: 69.4819
07-23 14:02:50.035: W/KKK(24257): ACTION MOVE DELTA X: -101.0 Y: 0.0
07-23 14:02:50.065: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.065: W/KKK(24257): ACTION MOVE BEFORE X: 172.46155 Y: 69.4819
07-23 14:02:50.065: W/KKK(24257): ACTION MOVE AFTER X: 69.128845 Y: 68.703705
07-23 14:02:50.065: W/KKK(24257): ACTION MOVE DELTA X: 103.3327 Y: 0.77819824
07-23 14:02:50.075: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.075: W/KKK(24257): ACTION MOVE BEFORE X: 69.128845 Y: 68.703705
07-23 14:02:50.085: W/KKK(24257): ACTION MOVE AFTER X: 170.1532 Y: 68.04462
07-23 14:02:50.085: W/KKK(24257): ACTION MOVE DELTA X: -101.02435 Y: 0.65908813
07-23 14:02:50.115: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.115: W/KKK(24257): ACTION MOVE BEFORE X: 170.1532 Y: 68.04462
07-23 14:02:50.115: W/KKK(24257): ACTION MOVE AFTER X: 70.46777 Y: 68.483154
07-23 14:02:50.115: W/KKK(24257): ACTION MOVE DELTA X: 99.685425 Y: -0.4385376
07-23 14:02:50.135: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.135: W/KKK(24257): ACTION MOVE BEFORE X: 70.46777 Y: 68.483154
07-23 14:02:50.135: W/KKK(24257): ACTION MOVE AFTER X: 169.46777 Y: 68.483154
07-23 14:02:50.135: W/KKK(24257): ACTION MOVE DELTA X: -99.0 Y: 0.0
07-23 14:02:50.165: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.165: W/KKK(24257): ACTION MOVE BEFORE X: 169.46777 Y: 68.483154
07-23 14:02:50.165: W/KKK(24257): ACTION MOVE AFTER X: 70.46777 Y: 68.483154
07-23 14:02:50.165: W/KKK(24257): ACTION MOVE DELTA X: 99.0 Y: 0.0
07-23 14:02:50.185: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.185: W/KKK(24257): ACTION MOVE BEFORE X: 70.46777 Y: 68.483154
07-23 14:02:50.185: W/KKK(24257): ACTION MOVE AFTER X: 169.46777 Y: 68.483154
07-23 14:02:50.185: W/KKK(24257): ACTION MOVE DELTA X: -99.0 Y: 0.0
07-23 14:02:50.225: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.225: W/KKK(24257): ACTION MOVE BEFORE X: 169.46777 Y: 68.483154
07-23 14:02:50.225: W/KKK(24257): ACTION MOVE AFTER X: 70.46777 Y: 68.483154
07-23 14:02:50.225: W/KKK(24257): ACTION MOVE DELTA X: 99.0 Y: 0.0
07-23 14:02:50.235: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.235: W/KKK(24257): ACTION MOVE BEFORE X: 70.46777 Y: 68.483154
07-23 14:02:50.235: W/KKK(24257): ACTION MOVE AFTER X: 169.46777 Y: 68.483154
07-23 14:02:50.235: W/KKK(24257): ACTION MOVE DELTA X: -99.0 Y: 0.0
07-23 14:02:50.285: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.285: W/KKK(24257): ACTION MOVE BEFORE X: 169.46777 Y: 68.483154
07-23 14:02:50.295: W/KKK(24257): ACTION MOVE AFTER X: 70.46777 Y: 68.483154
07-23 14:02:50.295: W/KKK(24257): ACTION MOVE DELTA X: 99.0 Y: 0.0
07-23 14:02:50.305: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.305: W/KKK(24257): ACTION MOVE BEFORE X: 70.46777 Y: 68.483154
07-23 14:02:50.305: W/KKK(24257): ACTION MOVE AFTER X: 169.46777 Y: 68.483154
07-23 14:02:50.305: W/KKK(24257): ACTION MOVE DELTA X: -99.0 Y: 0.0
07-23 14:02:50.345: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.345: W/KKK(24257): ACTION MOVE BEFORE X: 169.46777 Y: 68.483154
07-23 14:02:50.345: W/KKK(24257): ACTION MOVE AFTER X: 70.46777 Y: 68.483154
07-23 14:02:50.345: W/KKK(24257): ACTION MOVE DELTA X: 99.0 Y: 0.0
07-23 14:02:50.365: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.365: W/KKK(24257): ACTION MOVE BEFORE X: 70.46777 Y: 68.483154
07-23 14:02:50.365: W/KKK(24257): ACTION MOVE AFTER X: 169.46777 Y: 68.483154
07-23 14:02:50.365: W/KKK(24257): ACTION MOVE DELTA X: -99.0 Y: 0.0
07-23 14:02:50.375: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.375: W/KKK(24257): ACTION MOVE BEFORE X: 169.46777 Y: 68.483154
07-23 14:02:50.375: W/KKK(24257): ACTION MOVE AFTER X: 70.46777 Y: 68.483154
07-23 14:02:50.375: W/KKK(24257): ACTION MOVE DELTA X: 99.0 Y: 0.0
07-23 14:02:50.395: W/KKK(24257): ONTOUCH EVENT: 2
07-23 14:02:50.395: W/KKK(24257): ACTION MOVE BEFORE X: 70.46777 Y: 68.483154
07-23 14:02:50.395: W/KKK(24257): ACTION MOVE AFTER X: 169.46777 Y: 68.483154
07-23 14:02:50.395: W/KKK(24257): ACTION MOVE DELTA X: -99.0 Y: 0.0
07-23 14:02:50.395: W/KKK(24257): ONTOUCH EVENT: 1
最佳答案
解:
我变了
motionEvent.getX();
至
motionEvent.getRawX();
与Y座标方法类似,
而且效果很好。