Android初級開發第九講--動畫



      本文來自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可以對以上四種補間動畫類型進行組合,製造出更加炫酷的效果。


話外:通過AnimationSet可以將上述4種動畫,使用AnimatorInflater和ObjectAnimator,聯合起來一起(togethor)或者順序(sequentially)執行,做更復雜的效果。


動畫修飾:

  AccelerateDecelerateInterpolator 在動畫開始與結束的地方速率改變比較慢,在中間的時候加速

  AccelerateInterpolator  在動畫開始的地方速率改變比較慢,然後開始加速

  AnticipateInterpolator 開始的時候向後然後向前甩

  AnticipateOvershootInterpolator 開始的時候向後然後向前甩一定值後返回最後的值

  BounceInterpolator   動畫結束的時候彈起

  CycleInterpolator 動畫循環播放特定的次數,速率改變沿着正弦曲線

  DecelerateInterpolator 在動畫開始的地方快然後慢

  LinearInterpolator   以常量速率改變

  OvershootInterpolator    向前甩一定值後再回到原來位置




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章