- Tween animation補間動畫,主要是通過對控件實現(xiàn)透明度(alpha)葫慎、尺寸(scale)摇幻、位置(translate)、旋轉rotate)進行改變,通過集合(set)的方式,實現(xiàn)連續(xù)的動畫效果
- Frame animation
Tween動畫
1剪况、直接子類
- AlphaAnimation;控制一個對象的透明度播放的動畫
- RotateAnimation;控制一個對象旋轉的動畫
- ScaleAnimation;控制一個對象尺寸的動畫
- TranslateAnimation;控制一個對象位置的動畫
2探遵、監(jiān)聽器
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
3番挺、屬性及其對應方法(所有子類都擁有)
android:detachWallpaper: 是否在壁紙上運行
android:duration: 動畫時長姆坚,單位毫秒。
android:fillAfter: 設置為true膏蚓,控件動畫結束時將保持動畫最后一幀(xml文件中瓢谢,需要設置在set便簽才生效)。
android:fillBefore: 設置為true驮瞧,控件動畫結束時將保持動畫開始第一幀(感覺很坑爹氓扛,設置true和false還有刪除這個屬性,效果都一樣)论笔。
android:fillEnabled: 效果和fillBefore一樣(同樣坑爹采郎,經(jīng)測試這個屬性可有可無,求打臉狂魔。
android:interpolator: 插值器蒜埋。設置動畫速率的變化(譬如加速、減速最楷、勻速等)整份,后面詳說待错。
android:repeatCount: 動畫重復次數(shù)
ndroid:repeatMode: 重復模式,有reverse(倒序)和restart(重復)兩種烈评,必須配合repeatCount一起使用
android:startOffset: 延遲一定毫秒之后才開始動畫
-
android:zAdjustment: 表示被設置動畫的內容在動畫運行時在Z軸上的位置火俄,有以下三個值
- normal 默認值,保持內容在Z軸上的位置不變
- top 保持在Z周最上層
- bottom 保持在Z軸最下層
4讲冠、Interpolator
主要實現(xiàn)動畫的速率變化烛占,interpolator作為一個接口,然后抽象類BaseInterpolator實現(xiàn)Interpolator接口,在BaseInterpolator的子類就是android一系列Android提供的插值器沟启。
用法:
- 在XML的標簽下設置:android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
- 在JAVA代碼中使用:animation.setInterpolator(new AccelerateDecelerateInterpolator());
以下提供android 所有的插值器
- AccelerateDecelerateInterpolator:開始和結束速度慢,中間部分加速
- AccelerateInterpolator:開始緩慢犹菇,然后加速
- AnticipateInterpolator:開始后退德迹,然后前進
- AnticipateOvershootInterpolator:開始后退,然后前進揭芍,直到超出目標值胳搞,再后退至目標值
- BounceInterpolator:在結束時彈跳
- CycleInterpolator:在指定的周期內重復動畫,速度變化遵循正弦規(guī)律
- DecelerateInterpolator:開始加速称杨,結束緩慢
- LinearInterpolator:勻速
- OvershootInterpolator:前進肌毅,直到超出目標值,再后退至目標值
- PathInterpolator: 根據(jù)路勁變化改變速率
5姑原、AnimationSet
在res/anim/.xml文件下實現(xiàn)多種動畫(透明度悬而、尺寸、位置锭汛、旋轉)
6笨奠、相關代碼實現(xiàn)
-
java代碼實現(xiàn)動畫
private void alpha() { AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); alphaAnimation.setDuration(2000); alphaAnimation.setFillAfter(true); imv.setAnimation(alphaAnimation); //imv.startAnimation(alphaAnimation); } private void rote() { RotateAnimation rotateAnimation = new RotateAnimation(0, 720f); rotateAnimation.setDuration(2000); rotateAnimation.setFillAfter(true); imv2.setAnimation(rotateAnimation); } private void scale() { ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 2f, 0f, 3f); scaleAnimation.setDuration(3000); scaleAnimation.setFillAfter(true); imv.setAnimation(scaleAnimation); } private void translate() { TranslateAnimation animation = new TranslateAnimation(0f, 200f, 0f, 300f); animation.setDuration(3000); animation.setFillAfter(true); imv.setAnimation(animation); }
-
xml實現(xiàn)動畫
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:shareInterpolator="false"> <scale android:duration="1500" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="0.5" /> <set android:duration="2000" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:startOffset="1500"> <scale android:duration="1500" android:fromXScale="1.5" android:fromYScale="0.6" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.0" android:toYScale="0.0" /> <rotate android:fromDegrees="0.0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-45" /> </set> </set>
java中引用res文件
private void animationSet(){
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_set);
imv.startAnimation(animation);
}