本文來自 http://blog.csdn.net/liuxian13183/
,引用必須注明出處!
Android中動畫的應用,在應用管理軟體、購物軟體、追星軟體等比較廣泛;比如常見的進度條設計,此處尤其指圓形的那種。比如清理小火箭,從下向上飛出;比如清理軟體提示,由深色漸變成淺色。這些都是動畫的應用場景。
Android動畫分為兩種,一種叫幀動畫,就像flash一樣,學名Frame,進度條一般使用這種;另一種叫補間動畫,學名Tween,可以移動位置、變化顔色、變換大小、翻轉變化。
先說幀動畫
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@mipmap/icon_s01"
android:duration="30" />
<item
android:drawable="@mipmap/icon_s02"
android:duration="30" />
<item
android:drawable="@mipmap/icon_s03"
android:duration="30" />
</animation-list>
onshot的意思是,true隻播放一遍,false循環播放;item項指每隔duration展示的圖檔
應用:
先寫布局
<ImageView
android:id="@+id/loading_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/anim_progress_round"/>
再執行代碼
ImageView loadingImage = child.findViewById(R.id.loading_iv);
AnimationDrawable animation = (AnimationDrawable) loadingImage.getBackground();
if (animation.isRunning()) {
animation.stop();
}
animation.start();
獲得背景的AnimationDrawable對象,執行start方法即可。
補間動畫:
基類Animation,子類ScaleAnimation、AlphaAnimation、RotateAnimation、TranlateAnimation,分别用于大小變換、色彩變換、翻轉變換和位移變換。
常用方法有setDutation-設定運作時間,setFillAfter-設定運作結束是否保持最後狀态,setFillBefore-設定運作結束是否保質最初狀态,setRepeatCount-設定重複執行次數;setRepeatMode-設定重複類型,如REVERSE會倒着再執行repeatCount遍;setInterpolater-設定插補器,可以讓控件回彈,控制速度。
//accelerate_decelerate_interpolator先慢後快再慢linear_interpolator勻速decelerate_interpolator先快後慢
mAnimationTranslate.setInterpolator(mContext, android.R.anim.decelerate_interpolator);
例:
mAnimationTranslate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f,
Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
mAnimationTranslate.setDuration(3000);
mAnimationTranslate.setRepeatMode(Animation.ABSOLUTE);
mAnimationTranslate.setRepeatCount(0);
mRocketIv.startAnimation(mAnimationTranslate);
設定相關參數,執行startAnimation即可。
ScaleAnimation:fromXScale-X軸方向縮放比例,toXScale、fromYScale和toYScale同理;pivoteX起點X坐标,pivoteY同理。
AlphaAnimation:fromAlpha-起始透明度,toAlpha-最終透明度。
RotateAnimation:fromDegress-起始角度,toDegress-最終角度,正數為順時針,負數為逆時針。
TranslateAnimation:fromXType-起始位移類型,fromYType同理;fromXValue-起始X坐标,fromYValue同理;
從左邊折出來
ScaleAnimation animation = new ScaleAnimation(0f, 1f, 1f, 1f);
animation.setDuration(1000);//設定動畫持續時間
mChannelLayout.setAnimation(animation);
從左上角彈出來
ScaleAnimation animation = new ScaleAnimation(0f, 1f, 0f, 1f);
animation.setDuration(1000);//設定動畫持續時間
mChannelLayout.setAnimation(animation);
從左向右的動畫:
AnimationUtils.makeInAnimation(this, true);
從右回到左的動畫:
AnimationUtils.makeOutAnimation(this, false);
最後AnimationSet可以對以上四種補間動畫類型進行組合,制造出更加炫酷的效果。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iM18CXzV2Zh1WavwlbvlGdh1WauFWLkl2byRmbh9CX0NWZq9mcw9CXt92Yu4WY1lXZ1hXZrlmauk2apd3Lc9CX6MHc0RHaiojIsJye.gif)