ImageView 上的 OnTouchListener 不能正常工作?
OnTouchListener on an ImageView does not work correctly?
在我的应用程序中,我有一个图像视图(它是一个圆形,周围环绕着一个进度条 - 同样是圆形)。我将一个 onTouchListener 附加到它,因为我需要检查用户何时触摸图像视图以启动进度条动画,以及当用户的手指释放图像视图时我需要取消进度条动画。但是不知道为什么,只执行了action_down,不知道为什么。
这是我的布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cantDecideContainer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp">
<ImageView
android:id="@+id/grey_circle_bg"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:src="@drawable/perfect_grey_circle"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageView
android:id="@+id/cant_devide_black_circle_bg"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:src="@drawable/perfect_black_circle"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<com.studentsins.lust.UI.CircleProgressBar
android:id="@+id/cantDecideProgressBar"
android:layout_width="0dp"
android:layout_height="0dp"
app:progress="0"
app:progressBarThickness="3dp"
android:layout_alignTop="@+id/grey_circle_bg"
android:layout_alignLeft="@+id/grey_circle_bg"
android:layout_alignStart="@+id/grey_circle_bg"
android:layout_alignBottom="@+id/grey_circle_bg"
android:layout_alignRight="@+id/grey_circle_bg"
android:layout_alignEnd="@+id/grey_circle_bg"/>
</RelativeLayout>
这是我的代码:
private Animator.AnimatorListener progressBarAnimationListener = new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
mCantDecideProgressBar.setProgress(0);
}
@Override
public void onAnimationEnd(Animator animator) {
// Log.d(TAG, "onAnimationEnd");
mCantDecideProgressBar.setProgress(100);
//mNumSins.setText(numberOfSins+"");
}
@Override
public void onAnimationCancel(Animator animator) {}
@Override
public void onAnimationRepeat(Animator animator) {}
};
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
cantDecideProgressAnimator.cancel();
Log.d(TAG, "ACTION_UP canceled");
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cantDecideProgressAnimator.start();
Log.d(TAG, "ACTION_DOWN executed");
}
return false;
}
};
mCantDecideGg = (ImageView)view.findViewById(R.id.cant_devide_black_circle_bg);
mCantDecideGg.setOnTouchListener(onTouchListener);
mCantDecideProgressBar = (CircleProgressBar)view.findViewById(R.id.cantDecideProgressBar);
//set up the project animator to animate the progress bar from 0 to 100
cantDecideProgressAnimator = ObjectAnimator.ofFloat(mCantDecideProgressBar, "progress", 0.0f, 100.0f);
//add the animation listener to the progress animator to check when the progress has started,finished...
cantDecideProgressAnimator.addListener(progressBarAnimationListener);
//set the duration of the animation to 1.2 seconds
cantDecideProgressAnimator.setDuration(1200);
日志:
03-01 15:28:36.081 24093-24119/com.studentsins.lust I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 15:28:38.201 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
03-01 15:28:45.822 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
03-01 15:29:04.651 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
基于这个回答:
In onTouchEvent, ACTION_UP doesn't work
看来你应该 return true
在触摸事件中。
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
cantDecideProgressAnimator.cancel();
Log.d(TAG, "ACTION_UP canceled");
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cantDecideProgressAnimator.start();
Log.d(TAG, "ACTION_DOWN executed");
return true;
}
return false;
}
};
在我的应用程序中,我有一个图像视图(它是一个圆形,周围环绕着一个进度条 - 同样是圆形)。我将一个 onTouchListener 附加到它,因为我需要检查用户何时触摸图像视图以启动进度条动画,以及当用户的手指释放图像视图时我需要取消进度条动画。但是不知道为什么,只执行了action_down,不知道为什么。
这是我的布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cantDecideContainer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp">
<ImageView
android:id="@+id/grey_circle_bg"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:src="@drawable/perfect_grey_circle"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageView
android:id="@+id/cant_devide_black_circle_bg"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:src="@drawable/perfect_black_circle"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<com.studentsins.lust.UI.CircleProgressBar
android:id="@+id/cantDecideProgressBar"
android:layout_width="0dp"
android:layout_height="0dp"
app:progress="0"
app:progressBarThickness="3dp"
android:layout_alignTop="@+id/grey_circle_bg"
android:layout_alignLeft="@+id/grey_circle_bg"
android:layout_alignStart="@+id/grey_circle_bg"
android:layout_alignBottom="@+id/grey_circle_bg"
android:layout_alignRight="@+id/grey_circle_bg"
android:layout_alignEnd="@+id/grey_circle_bg"/>
</RelativeLayout>
这是我的代码:
private Animator.AnimatorListener progressBarAnimationListener = new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
mCantDecideProgressBar.setProgress(0);
}
@Override
public void onAnimationEnd(Animator animator) {
// Log.d(TAG, "onAnimationEnd");
mCantDecideProgressBar.setProgress(100);
//mNumSins.setText(numberOfSins+"");
}
@Override
public void onAnimationCancel(Animator animator) {}
@Override
public void onAnimationRepeat(Animator animator) {}
};
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
cantDecideProgressAnimator.cancel();
Log.d(TAG, "ACTION_UP canceled");
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cantDecideProgressAnimator.start();
Log.d(TAG, "ACTION_DOWN executed");
}
return false;
}
};
mCantDecideGg = (ImageView)view.findViewById(R.id.cant_devide_black_circle_bg);
mCantDecideGg.setOnTouchListener(onTouchListener);
mCantDecideProgressBar = (CircleProgressBar)view.findViewById(R.id.cantDecideProgressBar);
//set up the project animator to animate the progress bar from 0 to 100
cantDecideProgressAnimator = ObjectAnimator.ofFloat(mCantDecideProgressBar, "progress", 0.0f, 100.0f);
//add the animation listener to the progress animator to check when the progress has started,finished...
cantDecideProgressAnimator.addListener(progressBarAnimationListener);
//set the duration of the animation to 1.2 seconds
cantDecideProgressAnimator.setDuration(1200);
日志:
03-01 15:28:36.081 24093-24119/com.studentsins.lust I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 15:28:38.201 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
03-01 15:28:45.822 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
03-01 15:29:04.651 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
基于这个回答:
In onTouchEvent, ACTION_UP doesn't work
看来你应该 return true
在触摸事件中。
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
cantDecideProgressAnimator.cancel();
Log.d(TAG, "ACTION_UP canceled");
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cantDecideProgressAnimator.start();
Log.d(TAG, "ACTION_DOWN executed");
return true;
}
return false;
}
};