補間動畫 幀動畫 屬性動畫
TweeAnimation 補間動畫
只可以改變兩個關(guān)鍵幀之間的透明度,旋轉(zhuǎn),縮放,位移四個變化
AlphaAnimation
RotateAnimation
ScaleAnimation
TranslateAnimation
- 設(shè)置動畫的資源文件
補間動畫的資源文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
>
<scale android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:fillAfter="true"
android:duration="3000"
/>
</set>
animation=AnimationUtils.loadAnimation(this, R.anim.anim_1);//加載動畫資源
btn.setOnClickListener(this);//啟動動畫
- 直接在代碼中設(shè)置動畫
RotateAnimation animation = new RotateAnimation(0, 2700);//創(chuàng)建動畫
animation.setDuration(10000);//設(shè)置時長
animation.setRepeatCount(5);//設(shè)置重復(fù)次數(shù)
animation.setInterpolator(new AnticipateInterpolator());//設(shè)置插值器
animation.setAnimationListener(this);//設(shè)置監(jiān)聽
mButton1.startAnimation(animation);//啟動動畫
TimeInterpolator和TypeEvaluator
TimeInterpolator中文翻譯為時間插值器,它的作用是根據(jù)時間流逝的百分比來計算出當(dāng)前屬性值改變的百分比
系統(tǒng)預(yù)置的有LinearInterpolator(線性插值器:勻速動畫)、AccelerateDecelerateInterpolator(加速減速插值器:動畫兩頭慢中間快)和DecelerateInterpolator(減速插值器:動畫越來越慢)等
TypeEvaluator的中文翻譯為類型估值算法,它的作用是根據(jù)當(dāng)前屬性改變的百分比來計算改變后的屬性值
系統(tǒng)預(yù)置的有IntEvaluator(針對整型屬性)、FloatEvaluator(針對浮點型屬性)和ArgbEvaluator(針對Color屬性)
為補間動畫添加插值器Interpolator
- 幾種插值器
- 越來越快 AccelerateInterpolator()
- 越來越慢 DecelerateInterpolator()
- 先快后慢 AccelerateDecelerateInterpolator()
- 先后退一小步然后向前加速 AnticipateInterpolator()
- 快速到達終點超出一小步然后回到終點 OvershootInterpolator()
- 到達終點超出一小步然后回到終點 AnticipateOvershootInterpolator()
- 彈球效果,彈幾下回到終點 BounceInterpolator()
- 均勻速度 LinearInterpolator()
幀動畫
Frame Animation是順序播放事先做好的圖像,跟電影類似。不同于animation package,Android SDK提供了另外一個類AnimationDrawable來定義使用Frame Animation。
利用xml文件實現(xiàn):res/drawable-hdpi/frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/p1" android:duration="1000"/>
<item android:drawable="@drawable/p2" android:duration="1000"/>
<item android:drawable="@drawable/p3" android:duration="1000"/>
</animation-list>
- 使用幀動畫
AnimationDrawable anim = (AnimationDrawable)getResources().
getDrawable(R.drawable.frame);
textWidget = (TextView)findViewById(R.id.text_widget);
textWidget.setText("背景漸變動畫效果");
textWidget.setBackgroundDrawable(anim);
anim.start();
屬性動畫
ValueAnimation
ObjectAnimation
AnimatorSet
動畫的常見使用場景
在ViewGroup中控制子元素的出場效果
- 在anim文件夾中給ViewGroup設(shè)置動畫
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/animation_item"
android:animationOrder="normal"
android:delay="0.2">
<!--animation 給子控件設(shè)置動畫資源-->
<!--animationOrder 是定義子控件的出場順序-->
<!--delay 是定義兩個子控件的動畫延遲時間,0.2是延遲的倍數(shù)-->
</layoutAnimation>
- 在anim文件夾中給子控件設(shè)置動畫效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="@android:interpolator/anticipate"
android:shareInterpolator="true"
>
<alpha
android:fromAlpha="0"
android:toAlpha="1"/>
<translate
android:fromXDelta="500"
android:toXDelta="0"/>
</set>
- 這種方式也可在代碼中通過
LayoutAnimationController設(shè)置
Activity,F(xiàn)ragment的切換效果
- 設(shè)置activity的場景轉(zhuǎn)換動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:interpolator="@android:interpolator/anticipate"
android:shareInterpolator="true">
<rotate
android:fromDegrees="90"
android:toDegrees="0"/>
<translate
android:fromXDelta="500"
android:toXDelta="0"
/>
</set>
- 啟動activity的時候設(shè)置activity的轉(zhuǎn)場動畫,調(diào)用方法
overridePendingTransition,該方法必須在startActivity方法和finish方法之后調(diào)用才有效果
Intent intent = new Intent(this, SeconActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.activity_enter, R.anim.activity_out);
//分別設(shè)置進場和出場的動畫效果