補間動畫的實現 ObjectAnimator類

private static float currentValue;  //記錄當前旋轉的進度
private static long mDuration = 22000;  //旋轉的速度
//設置勻速轉動,AccelerateInterpolator爲加速效果、DecelerateInterpolator爲減速效果
LinearInterpolator lin = new LinearInterpolator();

*播放動畫的方法
 * 我也曾嘗試使用Animation這個工具類來實現iv_rotate.startAnimation(animation);旋轉的功能,
 * 但是出現了一個問題不能解決
 * 那就是每次暫停以後,圖片會歸位到初始的狀態,而不能記錄當前狀態
 * 要記錄當前的狀態,必須使用 ObjectAnimator這個類來定義動畫的操作對象
 *  */
public void rotateAnomaition(Long mDuration, LinearInterpolator linearInterpolator) {

    /* 圖片旋轉的動畫類的對象*/
    animation = ObjectAnimator.ofFloat(iv_rotate, "Rotation", currentValue - 359, currentValue);
    // 設置持續時間
    animation.setDuration(mDuration);

    animation.setInterpolator(linearInterpolator);
    // 設置循環播放
    animation.setRepeatCount(ObjectAnimator.INFINITE);
    // 設置動畫監聽
    animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // TODO Auto-generated method stub
            // 監聽動畫執行的位置,以便下次開始時,從當前位置開始
            // animation.getAnimatedValue()爲flort類型
            currentValue = (float) animation.getAnimatedValue();
        }
    });
    animation.start();  //開啓動畫,這個方法一般就是從起始位置開啓旋轉動畫
}
發佈了26 篇原創文章 · 獲贊 13 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章