Android中補間動畫的實現

下面要說的是補間動畫的實現。

補間動畫包括五種:

1、淡入淡出
2、旋轉
3、平移
4、縮放
5、組合
這些動畫效果可以在代碼中實現,也可以通過佈局文件來實現。
下面是我們在代碼中的實現:
package com.example.text02;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
	}

	// 淡入淡出
	public void dan(View view) {
		// 聲明一個AlphaAnimation對象,fromAlpha:起始透明度,toAlpha:結束透明度
		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.0f);
		// 設置透明度之間的間隔
		alphaAnimation.setDuration(2000);
		// 設置重複次數
		alphaAnimation.setRepeatCount(3);
		// 設置動畫的重複模式
		alphaAnimation.setRepeatMode(Animation.REVERSE);
		// 持久化保存結束時的樣子,結束時還原
		alphaAnimation.setFillAfter(true);
		// 啓動動畫
		// alphaAnimation.start();錯誤的啓動
		iv.startAnimation(alphaAnimation);
	}
	
	//旋轉
	public void turn(View view) {
		/**
		 * float fromDegree:起始角度 float toDegree:結束角度 第3,4參數:設置選中的中心點中x軸的大小
		 * 第5,6參數:設置旋轉的中心點中y軸的大小 x和y軸的交叉點是旋轉的中心點
		 */
		RotateAnimation rotateAnimation = new RotateAnimation(90, 270,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateAnimation.setDuration(3000);
		rotateAnimation.setRepeatMode(Animation.REVERSE);
		// rotateAnimation.setFillAfter(true);
		iv.startAnimation(rotateAnimation);
	}
	//移動
	public void move(View view) {
		/**
		 * 其中所有的value值表示的是相對於自身長度的百分比:percent,
		 * 移動距離 = 自身長度 * value;
		 */
		TranslateAnimation translateAnimation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
		translateAnimation.setDuration(2000);
		translateAnimation.setRepeatMode(Animation.REVERSE);
		translateAnimation.setFillAfter(true);
		iv.startAnimation(translateAnimation);
	}
	//縮放
	public void suofang(View view){
		/**
		 * 第1,2參數:表示x軸的起始和結束值(給出的值都是倍數關係,如果1則表示原來的大小,如果2.0f則表示原來大小的兩倍,如果0.5f則縮小了一倍)
		 * 同理第3,4參數,表示y軸的起始和結束的值
		 * 第5,6參數:第5個參數就是圓心的參照類型,如果類型是相對類型則第六個參數就是0.5f倍,x軸的倍數的大小
		 * 同理第7,8參數,第七個參數就是圓心的參照類型  第八個參數確定的是y軸倍數的大小
		 */
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setDuration(2000);
		scaleAnimation.setFillAfter(true);
		iv.startAnimation(scaleAnimation);
	}
	
	//組合
	public void zuhe(View view){
		//旋轉加縮放
		RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		
		//參數表示:如果爲true表示動畫的集合中每種動畫與集合採用相同的加速度
		//false:則表示每一種動畫都可以使用自己的加速器效果
		AnimationSet set = new AnimationSet(true);
		set.addAnimation(rotateAnimation);
		set.addAnimation(scaleAnimation);
		//設置集合的持續時間
		set.setDuration(2000);
		set.setRepeatMode(Animation.RESTART);
		set.setFillAfter(true);
		iv.setAnimation(set);
	}

}

運行結果:

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