其實前面有一篇關(guān)于動畫的詳細(xì)介紹了,寫這一篇博客就是為了更細(xì)致的介紹動畫太颤,多貼出來一些代碼谨垃,更直觀探討一下動畫的使用……
一、Animations介紹
Animations是一個實現(xiàn)Android UI界面動畫效果的API窖式,Animations提供了一系列的動畫效果,可以進(jìn)行旋轉(zhuǎn)动壤、縮放脖镀、淡入淡出等,這些效果可以應(yīng)用在絕大多數(shù)的控件中狼电。
二蜒灰、Animations的分類
Animations從總體上可以分為三大類:
1 . Tweened Animations:該類Animations提供了旋轉(zhuǎn)、移動肩碟、伸展和淡出等效果强窖。Alpha——淡入淡出,Scale——縮放效果削祈,Rotate——旋轉(zhuǎn)翅溺,Translate——移動效果。
2 . Frame-by-frame Animations:這一類Animations可以創(chuàng)建一個Drawable序列髓抑,這些Drawable可以按照指定的時間間歇一個一個的顯示咙崎。
3 . Property Animation : 故名思議就是通過動畫的方式改變對象的屬性了. 屬性動畫就是,動畫的執(zhí)行類來設(shè)置動畫操作的對象的屬性吨拍、持續(xù)時間褪猛,開始和結(jié)束的屬性值,時間差值等羹饰,然后系統(tǒng)會根據(jù)設(shè)置的參數(shù)動態(tài)的變化對象的屬性伊滋。
三、Animations的使用方法(代碼中使用)
Animations extends Object implements Cloneable 使用TweenedAnimations的步驟:
1.創(chuàng)建一個AnimationSet對象(Animation子類)队秩;
2.增加需要創(chuàng)建相應(yīng)的Animation對象笑旺;
3.更加項目的需求,為Animation對象設(shè)置相應(yīng)的數(shù)據(jù)馍资;
4.將Animatin對象添加到AnimationSet對象當(dāng)中筒主;
5.使用控件對象開始執(zhí)行AnimationSetTweened
Animations的分類
1、Alpha:淡入淡出效果
2、Scale:縮放效果
3乌妙、Rotate:旋轉(zhuǎn)效果
4色洞、Translate:移動效果
Animation的四個子類:
AlphaAnimation、TranslateAnimation冠胯、ScaleAnimation、RotateAnimation
四锦针、代碼具體使用
Tween Animations的通用方法
1荠察、setDuration(long durationMills) 設(shè)置動畫持續(xù)時間(單位:毫秒)
2、setFillAfter(Boolean fillAfter) 如果fillAfter的值為true,則動畫執(zhí)行后奈搜,控件將停留在執(zhí)行結(jié)束的狀態(tài)
3悉盆、setFillBefore(Boolean fillBefore) 如果fillBefore的值為true,則動畫執(zhí)行后馋吗,控件將回到動畫執(zhí)行之前的狀態(tài)
4焕盟、setStartOffSet(long startOffSet) 設(shè)置動畫執(zhí)行之前的等待時間
5、setRepeatCount(int repeatCount) 設(shè)置動畫重復(fù)執(zhí)行的次數(shù)
(一)代碼中使用Animation
漸變動畫
/**
* 漸變動畫
*/
public static void alphaAction(View view) {
//創(chuàng)建一個AnimationSet對象宏粤,參數(shù)為Boolean型脚翘,
//true表示使用Animation的interpolator,false則是使用自己的
AnimationSet animationSet = new AnimationSet(true)
//創(chuàng)建一個AlphaAnimation對象绍哎,參數(shù)從完全的透明度来农,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設(shè)置動畫執(zhí)行的時間
alphaAnimation.setDuration(500);
//將alphaAnimation對象添加到AnimationSet當(dāng)中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法執(zhí)行動畫
view.startAnimation(animationSet);
}
旋轉(zhuǎn)動畫
/**
* 旋轉(zhuǎn)動畫
*/
public static void rotateAction(View view) {
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1:從哪個旋轉(zhuǎn)角度開始
//參數(shù)2:轉(zhuǎn)到什么角度
//后4個參數(shù)用于設(shè)置圍繞著旋轉(zhuǎn)的圓的圓心在哪里
//參數(shù)3:確定x軸坐標(biāo)的類型,有ABSOLUT絕對坐標(biāo)崇堰、
RELATIVE_TO_SELF相對于自身坐標(biāo)沃于、RELATIVE_TO_PARENT相對于父控件的坐標(biāo)
//參數(shù)4:x軸的值,0.5f表明是以自身這個控件的一半長度為x軸
//參數(shù)5:確定y軸坐標(biāo)的類型
//參數(shù)6:y軸的值海诲,0.5f表明是以自身這個控件的一半長度為x軸
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
view.startAnimation(animationSet);
}
縮放動畫
/**
* 縮放動畫
*/
public static void scaleAction(View view) {
//參數(shù)1:x軸的初始值
//參數(shù)2:x軸收縮后的值
//參數(shù)3:y軸的初始值
//參數(shù)4:y軸收縮后的值
//參數(shù)5:確定x軸坐標(biāo)的類型
//參數(shù)6:x軸的值繁莹,0.5f表明是以自身這個控件的一半長度為x軸
//參數(shù)7:確定y軸坐標(biāo)的類型
//參數(shù)8:y軸的值,0.5f表明是以自身這個控件的一半長度為x軸
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation( 0, 0.1f, 0, 0.1f, Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 1);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
view.startAnimation(animationSet);
}
位移動畫
/**
* 位移動畫
*/
public static void translateAction(View view) {
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1~2:x軸的開始位置
//參數(shù)3~4:y軸的開始位置
//參數(shù)5~6:x軸的結(jié)束位置
//參數(shù)7~8:x軸的結(jié)束位置
TranslateAnimation translateAnimation = new
TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
view.startAnimation(animationSet);
}
下面貼出整理后的整個動畫工具類
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
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;
/**
* Created by 大軍 on 2016/5/3
*/
public class AnimationTools {
/**
* Tween Animations的通用方法
* 1特幔、setDuration(long durationMills)
* 設(shè)置動畫持續(xù)時間(單位:毫秒)
* 2咨演、setFillAfter(Boolean fillAfter)
* 如果fillAfter的值為true,則動畫執(zhí)行后,控件將停留在執(zhí)行結(jié)束的狀態(tài)
* 3蚯斯、setFillBefore(Boolean fillBefore)
* 如果fillBefore的值為true雪标,則動畫執(zhí)行后,控件將回到動畫執(zhí)行之前的狀態(tài)
* 4溉跃、setStartOffSet(long startOffSet)
* 設(shè)置動畫執(zhí)行之前的等待時間
* 5村刨、setRepeatCount(int repeatCount)
* 設(shè)置動畫重復(fù)執(zhí)行的次數(shù)
*/
// private static AnimationTools animationTools = null;
// private AnimationTools() {
//}
// public static AnimationTools instance() {
// if (animationTools == null) {
// synchronized (animationTools) {
// if (animationTools == null) {
// animationTools = new AnimationTools();
// }
// }
// }
// return animationTools;
// }
/**
* 漸變動畫
*/
public static void alphaAction(View view) {
//創(chuàng)建一個AnimationSet對象,參數(shù)為Boolean型撰茎,
//true表示使用Animation的interpolator嵌牺,false則是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//創(chuàng)建一個AlphaAnimation對象,參數(shù)從完全的透明度,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設(shè)置動畫執(zhí)行的時間
alphaAnimation.setDuration(500);
//將alphaAnimation對象添加到AnimationSet當(dāng)中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法執(zhí)行動畫
view.startAnimation(animationSet);
}
public static void alphaAction(AnimationSet animationSet) {
//創(chuàng)建一個AlphaAnimation對象逆粹,參數(shù)從完全的透明度募疮,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設(shè)置動畫執(zhí)行的時間
alphaAnimation.setDuration(500);
//將alphaAnimation對象添加到AnimationSet當(dāng)中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法執(zhí)行動畫
}
/**
* 旋轉(zhuǎn)動畫
*/
public static void rotateAction(View view) {
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1:從哪個旋轉(zhuǎn)角度開始
//參數(shù)2:轉(zhuǎn)到什么角度
//后4個參數(shù)用于設(shè)置圍繞著旋轉(zhuǎn)的圓的圓心在哪里
//參數(shù)3:確定x軸坐標(biāo)的類型,有ABSOLUT絕對坐標(biāo)僻弹、RELATIVE_TO_SELF相對于自身坐標(biāo)阿浓、RELATIVE_TO_PARENT相對于父控件的坐標(biāo)
//參數(shù)4:x軸的值,0.5f表明是以自身這個控件的一半長度為x軸
//參數(shù)5:確定y軸坐標(biāo)的類型
//參數(shù)6:y軸的值蹋绽,0.5f表明是以自身這個控件的一半長度為x軸
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
view.startAnimation(animationSet); } public static void
rotateAction(AnimationSet animationSet) { RotateAnimation
rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
}
/**
* 縮放動畫
*/
public static void scaleAction(View view) {
//參數(shù)1:x軸的初始值
//參數(shù)2:x軸收縮后的值
//參數(shù)3:y軸的初始值
//參數(shù)4:y軸收縮后的值
//參數(shù)5:確定x軸坐標(biāo)的類型
//參數(shù)6:x軸的值芭毙,0.5f表明是以自身這個控件的一半長度為x軸
//參數(shù)7:確定y軸坐標(biāo)的類型
//參數(shù)8:y軸的值,0.5f表明是以自身這個控件的一半長度為x軸
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation( 0, 0.1f, 0, 0.1f, Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 1);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
view.startAnimation(animationSet); } public static void
scaleAction(AnimationSet animationSet) { ScaleAnimation
scaleAnimation = new ScaleAnimation( 0, 0.1f, 0, 0.1f,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 1);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
}
/**
* 位移動畫
*/
public static void translateAction(View view) {
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1~2:x軸的開始位置
//參數(shù)3~4:y軸的開始位置
//參數(shù)5~6:x軸的結(jié)束位置
//參數(shù)7~8:x軸的結(jié)束位置
TranslateAnimation translateAnimation = new
TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
view.startAnimation(animationSet);
}
public static void translateAction(AnimationSet animationSet) {
TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
}
/**
* 屬性動畫
*/
public void propertyValuesHolder(View view) {
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start();
}
}
(二)在xml中使用Animations
1.在res文件夾下建立一個anim文件夾卸耘; 2.創(chuàng)建xml文件退敦,并首先加入set標(biāo)簽,更改標(biāo)簽如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
</set>
3.在該標(biāo)簽當(dāng)中加入rotate蚣抗,alpha侈百,scale或者translate標(biāo)簽;
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
4.在代碼當(dāng)中使用AnimationUtils當(dāng)中裝載xml文件翰铡,并生成Animation對象钝域。因為Animation是AnimationSet的子類,所以向上轉(zhuǎn)型锭魔,用Animation對象接收网梢。
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.alpha); // 啟動動畫
image.startAnimation(animation);
貼出具體代碼
1、 alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<!-- fromAlpha和toAlpha是起始透明度和結(jié)束時透明度 -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
</set>
2赂毯、 rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<!-- fromDegrees:開始的角度 toDegrees:結(jié)束的角度战虏,+表示是正的 pivotX:用于設(shè)置旋轉(zhuǎn)時的x軸坐標(biāo) 例 1)當(dāng)值為"50",表示使用絕對位置定位 2)當(dāng)值為"50%"党涕,表示使用相對于控件本身定位 3)當(dāng)值為"50%p"烦感,表示使用相對于控件的父控件定位 pivotY:用于設(shè)置旋轉(zhuǎn)時的y軸坐標(biāo) -->
<rotate android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
3、 scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<!-- 起始x軸坐標(biāo) 止x軸坐標(biāo) 始y軸坐標(biāo) 止y軸坐標(biāo) 軸的坐標(biāo) 軸的坐標(biāo) -->
<scale android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
4膛堤、 translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<!-- 始x軸坐標(biāo) 止x軸坐標(biāo) 始y軸坐標(biāo) 止y軸坐標(biāo) -->
<translate android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"/>
</set>
5手趣、 Java文件
import android.app.Activity;importandroid.os.Bundle;
import android.view.View;importandroid.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;import android.widget.Button;
import android.widget.ImageView;
public class Animation1Activity extends Activity {
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
private ImageView image = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotateButton = (Button) findViewById(R.id.rotateButton);
scaleButton = (Button) findViewById(R.id.scaleButton);
alphaButton = (Button) findViewById(R.id.alphaButton);
translateButton = (Button) findViewById(R.id.translateButton);
image = (ImageView) findViewById(R.id.image);
rotateButton.setOnClickListener(newRotateButtonListener());
scaleButton.setOnClickListener(newScaleButtonListener());
alphaButton.setOnClickListener(newAlphaButtonListener());
translateButton.setOnClickListener(newTranslateButtonListener();
}
class AlphaButtonListener implementsOnClickListener {
public void onClick(View v) {
// 使用AnimationUtils裝載動畫配置文件
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.alpha);
// 啟動動畫
image.startAnimation(animation);
}
}
class RotateButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.rotate);
image.startAnimation(animation);
}
}
class ScaleButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.scale);
image.startAnimation(animation);
}
}
class TranslateButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation =AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.translate);
image.startAnimation(animation);
}
}
}