視差特效
先要設置imageview的scaleType屬性為centerCrop
動態的改變一個控件的寬高可以在屬性動畫中不斷修改布局參數的值,然后調用該控件的iv_header.requestLayout();方法
// 把當前的頭布局的高度currentHeight恢復到初始高度orignalHeight final int currentHeight = iv_header.getHeight(); // 300 -> 160 ValueAnimator animator = ValueAnimator.ofInt(currentHeight, orignalHeight); // 動畫更新的監聽 animator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // 0.0 -> 1.0 // 獲取動畫執行過程中的分度值 float fraction = animation.getAnimatedFraction();// Integer evaluate = evaluate(fraction, currentHeight, orignalHeight); // 獲取中間的值 Integer animatedValue = (Integer) animation.getAnimatedValue(); System.out.注意點:可以用以下代碼在activity的onCreate()方法中獲取控件的寬高,但要注意,在在獲取完畢之后要移除監聽,否則會被反復調用賦值ivHeader.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 圖片的原始高度 drawableHeight = ivHeader.getDrawable().getIntrinsicHeight(); //Imageview的初始高度,用來做動畫 originalHeight = ivHeader.getHeight(); ivHeader.getViewTreeObserver().removeGlobalOnLayoutListener(this); } });新聞熱點
疑難解答