[TOC]
1. View/(Tween)補間動畫 Animation
View動畫時通過對View進行平移阐虚、旋轉拌阴、縮放和顯隱變換,從而產生動畫效果,是一種漸進式的動畫
1.1 平移 TranslateAnimation
Java代碼
//起點X因篇,Y 增加的X,Y
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
ta.setDuration(1000);
animationView.startAnimation(ta);
XML代碼 xml動畫文件路徑:res/anim/xxxanimation.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p"
android:fromXDelta="0%p"
android:toYDelta="90%p"
android:toXDelta="90%p"
android:duration="1000" />
Animation ta1 = AnimationUtils.loadAnimation(this, R.anim.translate);
animationView.startAnimation(ta1);
更復雜實現(xiàn)
//fromXType,fromXValue, toXType,toXValue,fromYType,fromYValue, toYType,toYValue //起點X方向參照值,起點X笔横,終點X方向參照值,終點X竞滓,起點Y方向參照值,起點Y,終點Y方向參照值,終點Y TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1, Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1);
參照值 描述 Animation.ABSOLUTE 具體的坐標值吹缔,指絕對的屏幕像素單位 Animation.RELATIVE_TO_SELF 相對自己的坐標值商佑,0.1f是指自己的坐標值乘以0.1 Animation.RELATIVE_TO_PARENT 相對父容器的坐標值,0.1f是指父容器的坐標值乘以0.1 簡單實現(xiàn)實際上是參照值全為Animation.ABSOLUTE的情況
1.2縮放 ScaleAnimation
Java代碼
//起始X倍數厢塘,最終X倍數茶没,起始Y倍數肌幽,最終X倍數
ScaleAnimation sa = new ScaleAnimation(4, 1, 4, 1);
sa.setDuration(2000);
baseAnimationView.startAnimation(sa);
XML代碼
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"/>
Animation sa1 = AnimationUtils.loadAnimation(this, R.anim.scale);
animationView.startAnimation(sa1);
更復雜實現(xiàn)
//起始X倍數,最終X倍數抓半,起始Y倍數喂急,最終X倍數,縮放模式笛求,0.5f自身中心廊移,縮放模式,0.5f自身中心 //ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2); ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); sa.setDuration(1000); animationView.startAnimation(sa);
1.3旋轉 RotateAnimation
Java代碼
//起始角度探入,最終角度狡孔,圓心X,Y坐標
//起始角度蜂嗽,最終角度步氏,旋轉模式,0.5f自身中心徒爹,旋轉模式荚醒,0.5f自身中心
//RotateAnimation ra = new RotateAnimation(0, 360, 100, 100);
RotateAnimation ra = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_PARENT,0.5f);
ra.setDuration(1000);
animationView.startAnimation(ra);
XML代碼
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration ="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" />
//調用
Animation ra1 = AnimationUtils.loadAnimation(this, R.anim.rorate);
animationView.startAnimation(ra1);
1.4 透明度 AlphaAnimation
Java代碼
//起始透明度,結束透明度
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
animationView.startAnimation(aa);
XML代碼
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0"
/>
//調用
Animation aa1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
animationView.startAnimation(aa1);
1.5 動畫集合
Java代碼
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 0);
ScaleAnimation sa = new ScaleAnimation(4, 2, 4, 2,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation aa = new AlphaAnimation(0, 1);
AnimationSet animationSet = new AnimationSet(true);
ta.setDuration(1000);
sa.setDuration(1000);
ra.setDuration(1000);
aa.setDuration(1000);
animationSet.addAnimation(ta);
animationSet.addAnimation(sa);
animationSet.addAnimation(ra);
animationSet.addAnimation(aa);
animationSet.setDuration(1000);
baseAnimationView.startAnimation(animationSet);
XML代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
android:fillAfter="false"
android:zAdjustment="normal"
>
<!-- 數字為絕對值隆嗅,x%p為父容器坐標值的x%值 -->
<translate
android:duration="1000"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="90%p"
android:toYDelta="90%p" />
<scale
android:duration="1000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
/>
<rotate
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
/>
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
調用
Animation a = AnimationUtils.loadAnimation(this, R.anim.base_animation);
baseAnimationView.startAnimation(a);
旋轉動畫與平移動畫做集合時界阁,旋轉坐標軸異常,即如何實現(xiàn) 滾動至指定位置動畫?
2. 幀動畫/AnimationDrawable
幀動畫是通過順序播放一系列圖像從而產生動畫效果胖喳,是一種簡單的圖片切換動畫
XML代碼 xml動畫文件路徑:res/drawable/xxxanimation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/p4" android:duration="100"/>
<item android:drawable="@drawable/p5" android:duration="100"/>
<item android:drawable="@drawable/p6" android:duration="100"/>
</animation-list>
調用
fAnimaitonView.setBackgroundResource(R.drawable.drawable_animation);
((AnimationDrawable) fAnimaitonView.getBackground()).start();
oneshot ture表示執(zhí)行一次 false表示循環(huán)執(zhí)行
3. 自定義動畫
我們可以通過繼承Animation類泡躯,重寫它的initialize和applyTransformation方法來實現(xiàn)我們想要的效果,中間涉及數學上矩陣變換的概念丽焊,但實際開發(fā)中幾乎用不到较剃,因此待后續(xù)有時間做基礎整理
4. 使用補充
4.1 動畫監(jiān)聽
View動畫不經常使用監(jiān)聽,但也有需要的時候
ta.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//啟動時回調
}
@Override
public void onAnimationEnd(Animation animation) {
//結束時回調
}
@Override
public void onAnimationRepeat(Animation animation) {
//重復時回調
}
});
4.2 插值器
插值器可以理解為動畫進行過程中不同階段時候設置動畫的速度值技健,如勻速運動写穴,加速運動,減速運動等
-
interpolator 設置插值器雌贱,默認android:anim/accelerate_decelerate_interpolator 加速減速
- @android:anim/accelerate_interpolator: 越來越快
- @android:anim/decelerate_interpolator:越來越慢
- @android:anim/accelerate_decelerate_interpolator:先快后慢
- @android:anim/anticipate_interpolator: 先后退一小步然后向前加速
- @android:anim/overshoot_interpolator:快速到達終點超出一小步然后回到終點
- @android:anim/anticipate_overshoot_interpolator:先后退一小步啊送,到達終點超出一小步然后回到終點
- @android:anim/bounce_interpolator:到達終點產生彈球效果,彈幾下回到終點
- @android:anim/linear_interpolator:均勻速度欣孤。
- @android:anim/cycle_interpolator 循環(huán)播放馋没,其速率為正弦曲線
shareInterpolator 設置集合是否與集合中的動畫共享一個插值器,不指定則使用單獨指定或是默認插值器
4.3 特殊使用場景
-
LayoutAnimation 為ViewGroup指定子元素的出場動畫
//Java代碼 Animation aSet = AnimationUtils.loadAnimation(this, R.anim.animation_set_1); LayoutAnimationController controller = new LayoutAnimationController(aSet); controller.setOrder(LayoutAnimationController.ORDER_RANDOM); controller.setDelay(0.5f); content_ll.setLayoutAnimation(controller); //XML代碼 <?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/animation_set_1"/> //指定動畫 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:shareInterpolator="true"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> </set> 使用時在ViewGroup中指定 android:layoutAnimation="@anim/layout_animation"
delay 表示子元素之間動畫間隔時間降传,實際時間為動畫周期*delay值
animationOrder 子元素動畫順序: normal-順序 reverse-逆序 random-隨機
animation 指定動畫
-
Activity切換動畫
使用方法:overridePendingTransition(int enterAnim,int exitAnim)
- 必須在startActivity或finish之后調用
- enterAnim-打開時的動畫資源篷朵,進入動畫
- exitAnim - 被暫停時的動畫資源,退出動畫
-
Fragment切換動畫
使用方法:setCustomAnimations(@AnimatorRes @AnimRes int enter, @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter, @AnimatorRes @AnimRes int popExit)
- 在FragmentManager開啟事務時進行操作
- enter Fragment被添加或綁定時所呈現(xiàn)動畫的資源id
- exit Fragment被移除或解綁時所呈現(xiàn)動畫的資源id
- popEnter Fragment從棧中彈出時所呈現(xiàn)動畫的資源id
- popExit Fragment壓入棧時所呈現(xiàn)動畫的資源id
v4包下的Fragment只能采用補間動畫婆排,因為屬性動畫是API11新引入的
-
共享元素動畫 5.0支持
示例代碼:
1. 在共享的控件元素中 加入屬性·`android:transitionName=“your_str”` 2.跳轉時設置 指定共享的視圖元素 ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), shareView, "image"); ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
- ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,View sharedElement, String sharedElementName)
activity-發(fā)起跳轉的Activity声旺,shareElement-共享的控件控硼,sharedElementName-定義的字符串
在Fragment如何使用共享元素轉場動畫
Fragment的startActivity()方法無法傳入ActivityOptionsCompat,所以需要使用
ActivityCompat.startActivity()來進行跳轉 - ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,View sharedElement, String sharedElementName)