2014-12-03 105 views
3

對於動畫我必須聽ViewPropertyAnimator的每一步。我使用AnimatorUpdateListener結合setUpdateListener
源:http://developer.android.com/reference/android/view/ViewPropertyAnimator.html在api級別使用setUpdateListener低於19


實施例如何使用它:

image.animate().translationY(transY).setDuration(duration).setUpdateListener(new AnimatorUpdateListener() { 

     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      // do my things 
     } 
}); 

查閱即時從A移動一個目的是B和必須detect一些事情而移動。現在setUpdateListener對此非常有幫助,而且這個代碼一切正常。但它需要api級別19.我真的想爲這個項目使用api level 14。 setUpdateListener有沒有其他選擇?

ViewPropertyAnimator.setUpdateListener

Call requires api level 19 (current min is 14) 
+1

你可以使用一個valueanimator?它看起來像它的addUpdateListener只需要api 11. – Whitney 2014-12-03 15:58:46

回答

3

隨着API級別19或以上,你可以說

image.animate() 
    .translationY(transY) 
    .setDuration(duration) 
    .setUpdateListener(new AnimatorUpdateListener() { 

     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      // do my things 
     } 

    }); 

隨着API級別11或以上,你可以求助於:

ObjectAnimator oa = ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, transY) 
            .setDuration(duration); 
oa.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     // do my things 
    } 
}); 
oa.start(); 

注意:雖然ViewProperyAnimator調用View.setHasTransientState()下的動畫視圖,ObjectAnimator沒有。這會在執行自定義操作時導致不同的行爲(即不會與ItemAnimatorRecyclerView項目動畫。

0

嘗試使用9OldAndroid lib中。它回遷蜂巢(Android 3.0的)動畫API的平臺的所有版本回1.0!

Rference鏈接 https://github.com/JakeWharton/NineOldAndroids/

+1

問題是,api 19包含api低於19的方法('setUpdateListener')。所以不推薦使用/ api級別19上丟失的舊方法不是問題。它的另一種方式。我在庫中搜索,但無法找到setUpdateListener的替代方法。 – 2014-12-03 12:27:42

3

下面是索爾特的回答在一處監聽器代碼和API的版本代碼級檢查的改進:

ValueAnimator.AnimatorUpdateListener updateListener = new ValueAnimator.AnimatorUpdateListener() { 
    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     // do my things 
    }  
}; 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
    image.animate() 
      .translationY(transY) 
      .setDuration(duration) 
      .setUpdateListener(updateListener); 
} else { 

    ObjectAnimator oa = ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, transY) 
            .setDuration(duration); 
    oa.addUpdateListener(updateListener); 
    oa.start(); 
} 
相關問題