【发布时间】:2018-05-17 06:35:32
【问题描述】:
在我的一个活动中,我有一个 ImageView,我将它设置为 onTouchListener,以便在屏幕上移动它,问题是当我在调试模式下测试它时一切正常,我看到 ImageView 是在屏幕上移动,我也可以点击它。但是,当我为发布模式创建签名 APK 并在同一设备上测试它时,它没有响应!
图像视图位于 RelativeLayout 内部并配置为:
<ImageView
android:id="@+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerInParent="true"
android:src="@drawable/app_launcher"
android:clickable="true"
/>
代码是:(此代码在 onCreate 方法中找到)
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
{
Toast.makeTest(MainActivity.this,
"imageView clicked!",Toast.LENGTH_LONG).show();
return true;
} else {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
initPosX= v.getX();
initPosY= v.getY();
initTouchX= event.getRawX();
initTouchY= event.getRawY();
lastAction= event.getAction();
return true;
case MotionEvent.ACTION_MOVE:
v.setX(initPosX+ (int)(event.getRawX() - initTouchX));
v.setY(initPosY+ (int)(event.getRawY() - initTouchY));
lastAction= event.getAction();
return true;
case MotionEvent.ACTION_UP:
v.setVisibility(View.VISIBLE);
lastAction= event.getAction();
return true;
}
}
return true;
}
});
我还有一个 GestureDetectorListener 来检测 singleTap:
private class GestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
}
【问题讨论】:
-
这似乎没有意义。尝试使用不同的设备或模拟器。
-
这就是为什么这是一个奇怪的行为!我用不同的设备尝试过。在调试时一切正常,在发布时没有
标签: android ontouchlistener ontouch