閱讀589 返回首頁    go 微軟 go windows


Android電視關閉的動畫效果

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

 

 

首先創建一個TVOffAnimation繼承於Animation: 

import android.graphics.Matrix;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class TVOffAnimation extends Animation {

	private int halfWidth;
	private int halfHeight;

	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {

		super.initialize(width, height, parentWidth, parentHeight);
		setDuration(500);
		setFillAfter(true);
		//保存View的中心點
		halfWidth = width / 2;
		halfHeight = height / 2;
		setInterpolator(new AccelerateDecelerateInterpolator());
		
	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {

		final Matrix matrix = t.getMatrix();
		if (interpolatedTime < 0.8) {
			matrix.preScale(1+0.625f*interpolatedTime, 1-interpolatedTime/0.8f+0.01f,halfWidth,halfHeight);
		}else{
			matrix.preScale(7.5f*(1-interpolatedTime),0.01f,halfWidth,halfHeight);
		}
	}
}

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中直接可以用了 

View img = findViewById(R.id.imageView);
button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				img.startAnimation(new TVOffAnimation());
			}
		});


最後更新:2017-04-02 16:47:36

  上一篇:go Android研究院之ListView原理學習與優化總結
  下一篇:go android圖片倒影控件ReflectionImage