2016-03-01 154 views
0

在我的應用程序中我有一個圖像視圖(這是一個圓形,它又被一個進度條包圍 - 圓形)。我將一個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

回答

3
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

相關問題