1. 程式人生 > >Android動畫控制元件之Animation

Android動畫控制元件之Animation

概述:

android的動畫效果包括:移動,漸變透明度,旋轉,縮放。
實現動畫的方式有兩種:在java程式碼中動態實現,在xml中靜態實現。

demo

動態實現:

    /*
     動畫的透明度漸變
      */
     AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0);//透明度從1到0
     alphaAnimation.setDuration(1000);//完成漸變的時間
     alphaAnimation.setStartOffset(200);//響應時間
     mImageViewAnim.startAnimation(alphaAnimation);//用一個ImageView載入animation,Image務必放入src或者載入過background
/* 動畫的移動 */ TranslateAnimation translateAnimation = //從一個座標到另一個座標的移動,引數依次為:起始點橫、縱座標,結束點橫、縱座標 new TranslateAnimation(-mImageViewAnim.getMeasuredWidth(),0,0,0); translateAnimation.setDuration(1000); translateAnimation.setStartOffset(200); mImageViewAnim.startAnimation(translateAnimation); /* 動畫的旋轉 */
RotateAnimation rotateAnimation = new RotateAnimation(0,360);//預設沿著左上角旋轉 rotateAnimation.setDuration(1000); rotateAnimation.setStartOffset(200); mImageViewAnim.startAnimation(rotateAnimation); /* 動畫的縮放 */ ScaleAnimation scaleAnimation = new ScaleAnimation(1f,2f,1f,2f);//水平尺寸變化,豎直尺寸變化
scaleAnimation.setDuration(1000); scaleAnimation.setStartOffset(200); mImageViewAnim.startAnimation(scaleAnimation);

動畫的合併載入,需要一個AnimationSet,將所有的animation載入進去:

    /**
      * 宣告一個AnimationSet,是一個儲存animation的集合
      * false:被此set載入的每個animation用自己的interpolator;
      * true:所有animation功用一個interpolator
      */
     AnimationSet animationSet = new AnimationSet(false);
     AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0);
     TranslateAnimation translateAnimation =
             new TranslateAnimation(0,0,mImageViewAnim.getMeasuredWidth(),mImageViewAnim.getMeasuredHeight());
     RotateAnimation rotateAnimation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF
             ,0.5f,RotateAnimation.RELATIVE_TO_SELF
             ,0.5f);//RELATIVE_TO_SELF的意思是位置相對於自己
     ScaleAnimation scaleAnimation = new ScaleAnimation(1f,2f,1f,2f);

     alphaAnimation.setDuration(1000);
     translateAnimation.setDuration(1000);
     rotateAnimation.setDuration(1000);
     scaleAnimation.setDuration(1000);
     //新增各個動畫
     animationSet.addAnimation(alphaAnimation);
     animationSet.addAnimation(translateAnimation);
     animationSet.addAnimation(rotateAnimation);
     animationSet.addAnimation(scaleAnimation);
     mImageViewAnim.startAnimation(animationSet);

這樣執行的效果就是所有動畫組合咋一起的效果。

動畫的靜態載入,需要在res目錄下新建一個資料夾anim,在裡面新建一個資原始檔,我的叫rotation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/anticipate_interpolator">
    <!-- cycle_interpolator:來回車輪效果;
    accelerate_decelerate_interpolator:先加速後減速
    bounce_interpolator:這個針對平移,下落反覆彈起效果
    anticipate_interpolator:準備效果-->
        <alpha
            android:fromAlpha="1"
            android:toAlpha="0"
            android:duration="2000">
            <!-- 從全顯示到不顯示,完成時間2000ms-->
            <!-- 從全顯示到不顯示,完成時間2000ms-->
        </alpha>
       <rotate
           android:fromDegrees="0"
           android:toDegrees="360"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="100"
           android:duration="2000"
           android:repeatCount="0">
           <!--引數依次是:從0度到360度相對自己中心旋轉,
           100ms後開始轉動,每次旋轉持續2000ms,旋轉(0+1)次-->
       </rotate>
        <scale
            android:fromXScale="1"
            android:toXScale="2"
            android:fromYScale="1"
            android:toYScale="2"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="2000">
            <!-- 水平從1倍大小變為兩倍,豎直從1倍大小變為兩倍
            以自己的中心點為軸,完成動畫需要2000ms-->
        </scale>
        <translate
            android:fromXDelta="0"
            android:toXDelta="0"
            android:fromYDelta="0"
            android:toYDelta="300"
            android:duration="2000">
            <!-- 初始水平位置:0
            終止水平位置:0
            初始豎直位置:0
            終止豎直位置:300-->
        </translate>
</set>

在java程式碼中呼叫這個資源:

        AnimationUtils utils = new AnimationUtils();
        Animation animation = utils.loadAnimation(getApplicationContext(),R.anim.rotation);
        mImageViewAnim.startAnimation(animation);

執行的效果是幾種xml中幾種動畫的綜合。

我們猿類工作壓力大,很需要有自己的樂趣,於是乎,我開通了音樂人賬號,以後的作品將會上傳到我的音樂人小站上。如果這篇部落格幫助到您,希望您能多關注,支援,鼓勵我將創作進行下去,同時也祝你能在工作和生活樂趣兩發麵都能出彩!

如果這篇部落格幫助到您,您可以完成以下操作:
在網易雲搜尋“星河河”->歌手->點選進入(您將進入我的網易雲音樂人賬號星河河)->關注我->多聽聽我的歌。
豆瓣音樂人地址:https://site.douban.com/chuxinghe/ 星河河