2014-01-08 46 views
1

我正在嘗試對特定視圖的高度變化進行動畫處理。我已經用Evaluator設置了一個ValueAnimator,並且我在動畫的每一遍都更新了LayoutParams。這又會觸發requestLayout()調用。然而,在佈局傳遞完成之前,動畫的下一個傳遞更新LayoutParams並觸發另一個requestLayout()。結果是在LogCat中輸出的警告以及動畫效果不佳。它似乎跳過了很多幀。使用ValueAnimator來爲requestLayout()中的LayoutParams結果設置動畫

ValueAnimator contentHeightAnimator = ValueAnimator.ofObject(new HeightEvaluator(mContentView), 
      mContentView.getMeasuredHeight(), (int) (getMeasuredHeight() - (actionBarHeight + destinationY))); 
    contentHeightAnimator.setDuration(duration); 
    contentHeightAnimator.setInterpolator(mInterpolator); 
    contentHeightAnimator.start(); 

... 

private static class HeightEvaluator extends IntEvaluator { 

    private View mView; 

    public HeightEvaluator(View v) { 
     this.mView = v; 
    } 

    @Override 
    public Integer evaluate(float fraction, Integer startValue, Integer endValue) { 
     int num = (Integer) super.evaluate(fraction, startValue, endValue); 
     ViewGroup.LayoutParams params = mView.getLayoutParams(); 
     params.height = num; 
     mView.setLayoutParams(params); 
     return num; 
    } 

} 

動畫布局變化的最佳方式是什麼?

回答

0

這裏有一個我用來製作動畫菜單分裂,以便其他,較低等級菜單可以插入

final int newBottomMargin = (int)(origBottomMargin + halfMargin); 
    final int newTopMargin = (int)(origTopMargin + halfMargin); 

    splitUp = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnShopWireless.getLayoutParams(); 
      params.bottomMargin = (int)(newBottomMargin * interpolatedTime); 
      btnShopWireless.setLayoutParams(params); 
     } 
    }; 

    splitDown = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnShopBundles.getLayoutParams(); 
      params.topMargin = (int)(newTopMargin * interpolatedTime); 
      btnShopBundles.setLayoutParams(params); 
     } 
    }; 
相關問題