Android 動(dòng)畫(huà)系統(tǒng)匯總

Android動(dòng)畫(huà)系統(tǒng)的種類(lèi):

1. 屬性動(dòng)畫(huà) (Property Animation)

2. 補(bǔ)間動(dòng)畫(huà) (Tween Animation)

3. 幀動(dòng)畫(huà) ? ? (Frame Animation)

4. Drawable


屬性動(dòng)畫(huà)(Property Animation):

可以改變控件對(duì)象屬性的對(duì)象,譬如控件的位置,透明度,旋轉(zhuǎn)等。

包含兩個(gè)基本的Animator:

1.ValueAnimator : 提供一個(gè)動(dòng)畫(huà)時(shí)序引擎,用來(lái)計(jì)算動(dòng)畫(huà)時(shí)序內(nèi),動(dòng)畫(huà)的值。

使用方式:
private void valueAnimatorTest(final View view) {?
? ? ? ValueAnimator anim = new ValueAnimator(); //創(chuàng)建ValueAnimator
? ? ? anim.setDuration(2000);? // 設(shè)置動(dòng)畫(huà)時(shí)常
? ? ? anim.setObjectValues(new My()); //設(shè)置自定義初始值
? ? ? anim.setEvaluator(new TypeEvaluator<My>() {?
? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? public My evaluate(float fraction, My startValue, My endValue) {
? ? ? ? ? ? ? ? ? ? System.out.println("fraction = " + fraction);
? ? ? ? ? ? ? ? ? ? // fraction:動(dòng)畫(huà)執(zhí)行時(shí)間片段
? ? ? ? ? ? ? ? ? ? My my = new My();? // My是一個(gè)實(shí)體類(lèi),可以自定義View的各個(gè)屬性值
? ? ? ? ? ? ? ? ? ? return my;
? ? ? ? ? ? ? }
? ? ? });
? ? anim.addUpdateListener(new AnimatorUpdateListener() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator valueAnimator) {
? ? ? ? ? ? ? ? ? ? ? My value = (My)valueAnimator.getAnimatedValue(); // 得到當(dāng)前動(dòng)畫(huà)片段的計(jì)算的自定義值,用來(lái)改變控件的屬性
? ? ? ? ? ? ? }
? ? ? });
? ? ? anim.start();
}

2. ObjectAnimator:是ValueAnimator的子類(lèi),這里對(duì)屬性進(jìn)行了一層邏輯實(shí)現(xiàn),使用者可以直接通過(guò)動(dòng)畫(huà)改變控件屬性。

translationX、translationY ? ? ? ? ? ?控制View相對(duì)于父布局平移距離
rotation、rotationX、rotationY ? ? 控制旋轉(zhuǎn)
scaleX、scaleY ? ? ? ? ? ? ? ? ? ? ? ? ? ?控制X 、Y軸方向的縮放
pivotX、pivotY ? ? ? ? ? ? ? ? ? ? ? ? ? ? 縮放和旋轉(zhuǎn)的支點(diǎn),默認(rèn)為View的中心
alpha ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?透明度(0~1)
x、y ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在父控件中的位置(注意和translationX、translationY的區(qū)分)

使用方式:
1. 控制單個(gè)屬性動(dòng)畫(huà)
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "scaleX", 1.0F,? 0.0F).setDuration(500);
anim.start();

2. 控制多個(gè)屬性動(dòng)畫(huà)
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "anywords", 1.0F,? 0.0F).setDuration(500);
anim.addUpdateListener(new AnimatorUpdateListener(){
? ? ? ? ? @Override
? ? ? ? ? public void onAnimationUpdate(ValueAnimator animation){
? ? ? ? ? ? ? ? ? ? float cVal = (Float) animation.getAnimatedValue();
? ? ? ? ? ? ? ? ? ? view.setAlpha(cVal);
? ? ? ? ? ? ? ? ? ? view.setScaleX(cVal);
? ? ? ? ? ? ? ? ? ? view.setScaleY(cVal);
? ? ? ? ? }
}
anim.start();


補(bǔ)間動(dòng)畫(huà) (Tween Animation)

比較簡(jiǎn)單,與屬性動(dòng)畫(huà)相比,只能改變控件的顯示狀態(tài),不能實(shí)際改變控件屬性值,主要有四中:

1. ScaleAnimation
2. RotateAnimation
3. AlphaAnimation
4. TranslateAnimation

補(bǔ)間動(dòng)畫(huà)相對(duì)簡(jiǎn)單,具體實(shí)現(xiàn)方式可參考其構(gòu)造方法即可。


幀動(dòng)畫(huà)? ? (Frame Animation)

顧名思義,就是與電影的播放原理一樣,逐幀顯示圖片達(dá)到動(dòng)畫(huà)的效果,實(shí)現(xiàn)方式:

1. 在res目錄下建立 drawable 文件夾
2. 在drawable文件夾目錄下創(chuàng)建? frame.xml 文件
3. frame.xml文件格式

<xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
? <item android:drawable="@drawable/pic1" android:duration="200"/>
? <item android:drawable="@drawable/pic2" android:duration="200"/>
? <item android:drawable="@drawable/pic3" android:duration="200"/>
</animation-list>

4. 應(yīng)用在ImageView中

<ImageView
? ? ....
? ?android:src = "@drawable/frame"
/>

5.? 代碼啟動(dòng)動(dòng)畫(huà)

AnimationDrawable animationDrawable = (AnimationDrawable) view.getDrawable();
animationDrawable.start();?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容