`

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.         });  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics