ImageView上的OnTouchListener无法正常工作?

问题描述:

在我的应用程序中我有一个图像视图(这是一个圆形,它又被一个进度条包围 - 圆形)。我将一个onTouchListener附加到它上面,因为我需要检查用户何时触摸图像视图以启动进度条动画,并且当用户的手指释放图像视图时,我需要取消进度条动画。但是,出于某种原因,只有action_down被执行,我不知道为什么。ImageView上的OnTouchListener无法正常工作?

enter image description here

这是我的布局:

<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 
+0

如果你检查Action_Cancel也会如何:if(event.getAction()== MotionEvent.ACTION_UP || event.getAction()== MotionEvent.ACTION_CANCEL) – Zaki

In onTouchEvent, ACTION_UP doesn't work 看来你应该在Touch Event中返回true

+0

噢,这很简单!非常感谢你!有效! –

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; 
} 

};

+0

谢谢你的回答。不过,@JeD是第一个。欣赏它,但。 :) –

+1

@GeorgiKoemdzhiev没问题,我希望这有助于:) – IgorB