OnTouchListener で非常に奇妙な問題が発生しています。昨夜、Galaxy Nexus を 4.1.1 にアップグレードしましたが、以前のすべての Android OS バージョンでまだ動作している次のコードがまったく動作しません。このリスナーの呼び出しには問題があると思います。以下はコードです。複雑な場合はロジックを無視して、ログの配置と出力を確認してください。
private OnTouchListener drag = new OnTouchListener() {
private float error_x = 0;
private float error_y = 0;
private int c = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("branch", "onTouch called " + ++c);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.i("branch", "1");
error_x = event.getX();
error_y = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
Log.i("branch", "2");
refreshTriangleColor(v);
root_layout.removeView(v);
root_layout.addView(v);
Point position = getViewPosition(v);
if (position != null) {
Log.i("branch", "3");
if (triangle_red
.pointLiesInTriangle(position.x, position.y)) {
Log.i("branch", "4");
int x = Math.round(event.getRawX());
int y = Math.round(event.getRawY());
if (triangle_red.pointLiesInTriangle(x, y)) {
Log.i("branch", "5");
setViewPosition(v,
Math.round(event.getRawX() - error_x),
Math.round(event.getRawY() - error_y));
} else {
Log.i("branch", "6");
long d = 0;
d = Math.round(android.util.FloatMath.sqrt((((position.x - event
.getRawX()) * (position.x - event.getRawX())) - ((position.y - event
.getRawY()) * (position.y - event.getRawY())))));
if (d >= Util.convertDip2Pixels(MAX_RESISTANCE,
MelodyTriangle_AppActivity.this)) {
Log.i("branch", "7");
setViewPosition(v,
Math.round(event.getRawX() - error_x),
Math.round(event.getRawY() - error_y));
vibrate();
} else {
Log.i("branch", "8");
Point nearest_point = triangle_red
.getNearestPointOnLine(new Point(x, y));
if (nearest_point != null) {
Log.i("branch", "9");
setViewPosition(
v,
Math.round(nearest_point.x
- error_x),
Math.round(nearest_point.y
- error_y));
}
}
}
} else {
Log.i("branch", "10");
setViewPosition(v,
Math.round(event.getRawX() - error_x),
Math.round(event.getRawY() - error_y));
}
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Log.i("branch", "11");
Point position = getViewPosition(v);
if (position != null) {
Log.i("branch", "12");
if (!triangle_red.pointLiesInTriangle(position.x,
position.y)) {
Log.i("branch", "13");
if (api.voices[0].isPlaying()) {
Log.i("branch", "14");
api.voices[0].stopPlaying();
}
animateToLayout(v, red_layout);
} else {
Log.i("branch", "15");
playVoice(v, position.x, position.y);
}
}
}
return true;
}
};
ドラッグしようとすると、上記のコードから次の出力が得られます。
07-18 16:28:53.897: I/branch(31176): onTouch called 1
07-18 16:28:53.897: I/branch(31176): 1
07-18 16:28:54.007: I/branch(31176): onTouch called 2
07-18 16:28:54.007: I/branch(31176): 2
07-18 16:28:54.007: I/branch(31176): onTouch called 3
07-18 16:28:54.007: I/branch(31176): 3
07-18 16:28:54.007: I/branch(31176): 10
07-18 16:28:56.077: I/branch(31176): onTouch called 4
07-18 16:28:56.077: I/branch(31176): 1
07-18 16:28:56.124: I/branch(31176): onTouch called 5
07-18 16:28:56.124: I/branch(31176): 2
07-18 16:28:56.124: I/branch(31176): onTouch called 6
07-18 16:28:56.124: I/branch(31176): 3
07-18 16:28:56.124: I/branch(31176): 10
07-18 16:29:00.764: I/branch(31176): onTouch called 7
07-18 16:29:00.764: I/branch(31176): 1
07-18 16:29:00.803: I/branch(31176): onTouch called 8
07-18 16:29:00.803: I/branch(31176): 2
07-18 16:29:00.803: I/branch(31176): onTouch called 9
07-18 16:29:00.803: I/branch(31176): 3
07-18 16:29:00.803: I/branch(31176): 10
気づいた問題は次のとおりです。
ドラッグがスムーズでないため、タッチが定期的に呼び出されていません。ビューをその位置からわずかに移動するには、ビューを汚す必要があるため、タッチに合わせて移動しません。一方、以前のバージョンでは通常です。
event.getAction() == MotionEvent.ACTION_UP は、ビューに触れて離したときにのみ true になります。ビューがタッチに沿って移動するため、以前のバージョンではほぼ毎回呼び出されていましたが、リリースするたびにこれが当てはまります。
何が間違っているのですか?
ありがとう