1. 程式人生 > >安卓中為View新增動畫效果(尺寸縮放、透明度漸變、旋轉、移動)

安卓中為View新增動畫效果(尺寸縮放、透明度漸變、旋轉、移動)

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;


/**
 * AnimationEffect 安卓中為View新增動畫效果
 * -----
 * 2015-10-23 上午11:09:04 
 * wangzhongyuan
 */
public class AnimationEffect
{
	/**
	 *  為View新增透明度變換效果
	 */
	public static void AddAlphaAni(View view)
	{
		AddAlphaAni(view, 0, 1, 1000, Animation.REVERSE, Animation.INFINITE);
	}
	
	/**
	 *  為View新增透明度變換效果,透明度從fromAlpha變化到toAlpha,變化持續時間durationMillis,重複模式repeatMode
	 */
	public static void AddAlphaAni(View view, float fromAlpha, float toAlpha, long durationMillis, int repeatMode, int repeatCount)
	{
		AlphaAnimation alphaAni = new AlphaAnimation(fromAlpha, toAlpha);
		alphaAni.setDuration(durationMillis);	// 設定動畫效果時間
		alphaAni.setRepeatMode(repeatMode);		// 重新播放
		alphaAni.setRepeatCount(repeatCount);	// 迴圈播放
		
		view.startAnimation(alphaAni);
	}
	
	/**
	 *  設定位移動畫效果,來回移動
	 */
	public static void setTransAni(View view, int x0, int x1, int y0, int y1, long durationMillis)
	{
		TranslateAnimation transAni = new TranslateAnimation(x0, x1, y0, y1);
		transAni.setDuration(durationMillis);
		transAni.setRepeatMode(Animation.REVERSE);
		transAni.setRepeatCount(Animation.INFINITE);
		
		view.startAnimation(transAni);
	}
	
	/**
	 *  設定旋轉動畫
	 */
	public static void setRotateAni(View view, float fromDegrees, float toDegrees, long time)
	{
		setRotateAni(view, fromDegrees, toDegrees, time, Animation.REVERSE, Animation.INFINITE, true);
	}
	
	/**
	 *  設定旋轉動畫, 從角度fromDegrees旋轉到toDegrees,旋轉速度durationMillis毫秒,重啟模式repeatModes,重啟次數repeatCount,是否勻速旋轉linear。
	 *  示例:AnimationEffect.setRotateAni(view, 0, 360, 1000, Animation.RESTART, Animation.INFINITE, true); // 控制view每秒旋轉360度
	 */
	public static void setRotateAni(View view, float fromDegrees, float toDegrees, long durationMillis, int repeatModes, int repeatCount, boolean linear)
	{
		RotateAnimation rotateAni = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		rotateAni.setDuration(durationMillis);
		rotateAni.setRepeatMode(repeatModes);
		rotateAni.setRepeatCount(repeatCount);
		
		if(linear) rotateAni.setInterpolator(new LinearInterpolator());	// 勻速旋轉
		
		view.startAnimation(rotateAni);
	}
	
	/**
	 *  為控制元件新增尺寸漸變動畫
	 */
	public static void setScaleAni(View V, float fromScale, float toScale, long ANITIME)
	{
		AnimationSet aniSet = new AnimationSet(true);
		// final int ANITIME = 500;
		
		// 尺寸變化動畫,設定尺寸變化
		ScaleAnimation scaleAni = new ScaleAnimation(fromScale, toScale, fromScale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAni.setDuration(ANITIME);	// 設定動畫效果時間
		aniSet.addAnimation(scaleAni);	// 將動畫效果新增到動畫集中
		
		V.startAnimation(aniSet);		// 新增光效動畫到控制元件
	}
	
	/**
	 *  為控制元件新增擴散光效
	 */
	public static void setLightExpendAni(View V)
	{
		AnimationSet aniSet = new AnimationSet(true);
		final int ANITIME = 1200;
		
		// 尺寸變化動畫,設定尺寸變化
		ScaleAnimation scaleAni = new ScaleAnimation(0.98f, 1.1f, 0.98f, 1.24f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAni.setDuration(ANITIME);				// 設定動畫效果時間
		scaleAni.setRepeatMode(Animation.RESTART);	// 重新播放
		scaleAni.setRepeatCount(Animation.INFINITE);// 迴圈播放
		aniSet.addAnimation(scaleAni);	// 將動畫效果新增到動畫集中
		
		// 透明度變化
		AlphaAnimation alphaAni = new AlphaAnimation(1f, 0.05f);
		alphaAni.setDuration(ANITIME);				// 設定動畫效果時間
		alphaAni.setRepeatMode(Animation.RESTART);	// 重新播放
		alphaAni.setRepeatCount(Animation.INFINITE);// 迴圈播放
		aniSet.addAnimation(alphaAni);	// 將動畫效果新增到動畫集中
		
		V.startAnimation(aniSet);		// 新增光效動畫到控制元件
	}
	
	/**
	 *  設定背景旋轉光效動畫
	 */
	public static void setLightAni(final View view)
	{
		AnimationListener listenser = new AnimationListener()
		{
			@Override
			public void onAnimationEnd(Animation animation)
			{
				view.clearAnimation();
				setRotateAni(view, 36, 3996, 110000, Animation.RESTART, Animation.INFINITE, true);
			}
			
			@Override
			public void onAnimationRepeat(Animation animation)
			{}
			
			@Override
			public void onAnimationStart(Animation animation)
			{}
		};
		
		AnimationSet set = new AnimationSet(true);
		
		// 漸現
		AlphaAnimation alphaAni = new AlphaAnimation(0.0f, 1f);
		alphaAni.setDuration(1000);	// 設定動畫效果時間
		set.addAnimation(alphaAni);
		
		// 旋轉
		RotateAnimation rotateAni = new RotateAnimation(0, 36, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		rotateAni.setDuration(1000);
		set.addAnimation(rotateAni);
		
		// 尺寸由小變大
		ScaleAnimation scaleAni = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAni.setDuration(1000);
		scaleAni.setAnimationListener(listenser);
		set.addAnimation(scaleAni);
		
		view.startAnimation(set);
	}
}