我试图在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;
};
};
logcat的:
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
答案 0 :(得分:1)
解决方案:
我已经改变了
motionEvent.getX();
到
motionEvent.getRawX();
与Y坐标方法类似,
并且效果很好。
答案 1 :(得分:0)
所有坐标都与视图相关。 如果您在TextView上设置onTouchListener,则表示您遇到了麻烦。 由于您的TextView位于手指下,因此TextView的每次移动都会为屏幕上的相同触摸位置生成不同的值。这就是为什么你的轻微手指动作会弄乱整个系统。
原始坐标是绝对的,所以它是一种出路,但是与父布局协调将更难(在移动和东西上达到界限)
您应该在FrameLayout中设置onTouchListener,这也是绝对的(因为它不会移动,屏幕上的一个具体点将始终产生相同的值),并修改onTouchListener以检查TexView是否触摸并触摸如果是的话 - 做一些拖动