Android電視關閉的動畫效果

老式電視機關閉的時候畫面一閃消失的那個效果: 

 

 

首先創建一個TVOffAnimation繼承於Animation: 
Java代碼  收藏代碼
  1. import android.graphics.Matrix;  
  2. import android.view.animation.AccelerateDecelerateInterpolator;  
  3. import android.view.animation.Animation;  
  4. import android.view.animation.Transformation;  
  5.   
  6. public class TVOffAnimation extends Animation {  
  7.   
  8.     private int halfWidth;  
  9.     private int halfHeight;  
  10.   
  11.     @Override  
  12.     public void initialize(int width, int height, int parentWidth,  
  13.             int parentHeight) {  
  14.   
  15.         super.initialize(width, height, parentWidth, parentHeight);  
  16.         setDuration(500);  
  17.         setFillAfter(true);  
  18.         //保存View的中心點  
  19.         halfWidth = width / 2;  
  20.         halfHeight = height / 2;  
  21.         setInterpolator(new AccelerateDecelerateInterpolator());  
  22.           
  23.     }  
  24.   
  25.     @Override  
  26.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  27.   
  28.         final Matrix matrix = t.getMatrix();  
  29.         if (interpolatedTime < 0.8) {  
  30.             matrix.preScale(1+0.625f*interpolatedTime, 1-interpolatedTime/0.8f+0.01f,halfWidth,halfHeight);  
  31.         }else{  
  32.             matrix.preScale(7.5f*(1-interpolatedTime),0.01f,halfWidth,halfHeight);  
  33.         }  
  34.     }  
  35. }  


interpolatedTime表示的是當前動畫的間隔時間 範圍是0-1 

那麼橫向來講前80%的時間我們要橫向拉伸到150%,縱向是直接減小,最後只留一條線。 
後20%的時間裏我們要橫向從150%壓縮至0%,縱向保持不變就好了,當橫向爲0的時候就全部消失了。 
可能大家對於1+0.625f*interpolatedTime, 1-interpolatedTime/0.8f+0.01f,7.5f*(1-interpolatedTime),0.01f 這4個值比較疑惑,其實很簡單,這是一個一次函數的函數值。 

然後在activity中直接可以用了 
Java代碼  收藏代碼
  1. View img = findViewById(R.id.imageView);  
  2. button.setOnClickListener(new OnClickListener() {  
  3.   
  4.             @Override  
  5.             public void onClick(View v) {  
  6.                 img.startAnimation(new TVOffAnimation());  
  7.             }  
  8.         });  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章