天天看點

Android 動畫的使用,以及組合動畫

點選下載下傳demo

效果圖:

Android 動畫的使用,以及組合動畫
Android 動畫的使用,以及組合動畫

一、Android 四種類型的animation

javaCode動畫類型 xml動畫類型 描述 屬性
AlphaAnimation alph 漸變透明

浮點型值:

fromAlpha:動畫起始時透明度

toAlpha:動畫結束時透明度

duration:動畫持續時間

說明:

     0.0表示完全透明

     1.0表示完全不透明

     以上取值0.0-1.0之間的float資料類型的數字

duration:    

      為長整形,時間機關為毫秒

ScaleAnimation scale 漸變尺寸伸縮

iinterpolator:指定一個動畫的插入器

我這裡列出三種動畫插入器:(可自行測試一下)

      accelerate_decelerate_interpolator  加速-減速

      accelerate_interpolator                     加速

      decelerate_interpolator                     減速

fromXScale:動畫起始時,x坐标上的伸縮尺寸

toXScale:動畫結束時,x坐标上的伸縮尺寸

fromYScale:動畫起始時Y坐标上的伸縮尺寸

toYScale:動畫結束時,Y坐标上的伸縮尺寸

說明:

       以上四個是資料浮點型數值,float

        0.0表示收縮到沒有

        1.0表示正常無伸縮

         值<1.0表示收縮

         值>1.0表示放大

pivotX:動畫相對于物件的x坐标的開始位置

pivotY:動畫相對于物件的Y坐标的開始位置

說明:

       以上兩個屬性值  :從0%-100%中取值

      50%為物件的X或Y方向坐标上的中點位置

duration:動畫持續時間(長整型數值,機關毫秒)

fillAfter:當設定為true,該動畫轉化在動畫結束後被應用

TranslateAnimation transtate 轉換位置移動

fromXDelta:動畫起始時,X坐标上的位置

toXDelta:動畫結束時,X坐标上的位置

fromYDelta:動畫起始時,Y坐标上的位置

toYDelta:動畫結束時,Y坐标上的位置

duration:動畫持續時間,時間以毫秒為機關

說明:

    沒有指定fromXType,toXType,fromYType,toYType時        候,預設是以自己為為相對參照物

RotateAnimation rotate 畫面旋轉

interpolator:指定一個動畫的插入器

        我這裡列出三種(可自行測試)

       accelerate_decelerate_interpolator:加速-減速

       accelerate_interpolator:加速

       decelerate_interpolator:減速

fromDegrees:動畫起始時物件的角度

toDegrees:動畫結束時物件旋轉的角度

說明:

       當角度為負數——表示逆時針旋轉

       當角度為正數——表示順時針旋轉

       [負數from——to正數:順時針旋轉]

       [負數from——to負數:逆時針旋轉]

       [正數from——to正數:順時針旋轉]

       [正數from——to負數:逆時針旋轉]

pivotX:動畫相對于物件的X坐标的開始的位置

pivotY:動畫相對于物件的Y坐标的開始位置

說明:

       以上兩個屬性值:從0%——100%中取值

      50%為物件的X或Y方向坐标上的中點位置

duration:動畫持續時間,機關為 

2.在javaCode中

二、在xml中的使用:在res下建立一個anim檔案夾,建立對應的xml動畫檔案

Android 動畫的使用,以及組合動畫

1.alpha.xml(漸變透明)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />


    <!--浮點型值:
 fromAlpha:動畫起始時透明度
 toAlpha:動畫結束時透明度
 duration:動畫持續時間
 說明:
      0.0表示完全透明
      1.0表示完全不透明
      以上取值0.0-1.0之間的float資料類型的數字
 duration:    
       為長整形,時間機關為毫秒-->

</set>
           

2.rotate.xml

​
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--畫面轉移旋轉動畫效果-->

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+350" />

    <!--
        rotate 旋轉動畫效果
          屬性:interpolator 指定一個動畫的插入器
                在我試驗過程中,使用android.res.anim中的資源時候發現
                有三種動畫插入器:
                   accelerate_decelerate_interpolator   加速-減速 動畫插入器
                   accelerate_interpolator               加速-動畫插入器
                   decelerate_interpolator               減速- 動畫插入器
                其他的屬于特定的動畫效果

          浮點數型值:
               fromDegrees 屬性為動畫起始時物件的角度
               toDegrees   屬性為動畫結束時物件旋轉的角度 可以大于360度


               說明:
                        當角度為負數——表示逆時針旋轉
                        當角度為正數——表示順時針旋轉
                        (負數from——to正數:順時針旋轉)
                        (負數from——to負數:逆時針旋轉)
                        (正數from——to正數:順時針旋轉)
                        (正數from——to負數:逆時針旋轉)

               pivotX     屬性為動畫相對于物件的X坐标的開始位置
               pivotY     屬性為動畫相對于物件的Y坐标的開始位置

               說明:        以上兩個屬性值 從0%-100%中取值
                            50%為物件的X或Y方向坐标上的中點位置

           長整型值:
               duration  屬性為動畫持續時間
               說明:       時間以毫秒為機關

       -->
</set>

​
           

3.scale.xml

​
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--漸變尺寸伸縮動畫-->

    <scale
        android:duration="3000"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.4"
        android:toYScale="1.4" />
    <!--
         尺寸伸縮動畫效果 scale
           屬性:interpolator 指定一個動畫的插入器
            在我試驗過程中,使用android.res.anim中的資源時候發現
            有三種動畫插入器:
                accelerate_decelerate_interpolator  加速-減速 動畫插入器
                accelerate_interpolator        加速-動畫插入器
                decelerate_interpolator        減速- 動畫插入器
            其他的屬于特定的動畫效果
          浮點型值:

                fromXScale 屬性為動畫起始時 X坐标上的伸縮尺寸
                toXScale   屬性為動畫結束時 X坐标上的伸縮尺寸

                fromYScale 屬性為動畫起始時Y坐标上的伸縮尺寸
                toYScale   屬性為動畫結束時Y坐标上的伸縮尺寸

                說明:
                     以上四種屬性值

                        0.0表示收縮到沒有
                        1.0表示正常無伸縮
                        值小于1.0表示收縮
                        值大于1.0表示放大

                pivotX     屬性為動畫相對于物件的X坐标的開始位置
                pivotY     屬性為動畫相對于物件的Y坐标的開始位置

                說明:
                        以上兩個屬性值 從0%-100%中取值
                        50%為物件的X或Y方向坐标上的中點位置

            長整型值:
                duration  屬性為動畫持續時間
                說明:   時間以毫秒為機關

            布爾型值:
                fillAfter 屬性 當設定為true ,該動畫轉化在動畫結束後被應用

    -->

</set>

​
           

4.translate.xml

​
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--位置移動動畫效果-->

    <translate
        android:duration="3000"
        android:fromXDelta="30"
        android:fromYDelta="30"
        android:toXDelta="-80"
        android:toYDelta="300" />
    <!--
         translate 位置轉移動畫效果
            整型值:
                fromXDelta 屬性為動畫起始時 X坐标上的位置
                toXDelta   屬性為動畫結束時 X坐标上的位置
                fromYDelta 屬性為動畫起始時 Y坐标上的位置
                toYDelta   屬性為動畫結束時 Y坐标上的位置
                注意:
                         沒有指定fromXType toXType fromYType toYType 時候,
                         預設是以自己為相對參照物
            長整型值:
                duration  屬性為動畫持續時間
                說明:   時間以毫秒為機關



        -->


</set>

​
           

xml中動畫效果的使用方式:

alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
  heartImg.startAnimation(alphaAnimation);
  alphaAnimation.setAnimationListener(this);
           

三、在javaCode中的使用

1.AlphaAniamtion(漸變透明)

//第一個參數:動畫開始時透明度,第二個參數:動畫結束時候透明
 alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
 alphaAnimation.setDuration(3000);//設定時間
 heartImg.startAnimation(alphaAnimation);//啟動動畫
 alphaAnimation.setAnimationListener(this);//設定監聽
           

2.ScaleAnimation(尺寸伸縮)

//第一個參數fromX為動畫起始時 X坐标上的伸縮尺寸    
//第二個參數toX為動畫結束時 X坐标上的伸縮尺寸     
//第三個參數fromY為動畫起始時Y坐标上的伸縮尺寸    
//第四個參數toY為動畫結束時Y坐标上的伸縮尺寸  
/*說明:
                    以上四種屬性值    
                    0.0表示收縮到沒有 
                    1.0表示正常無伸縮     
                    值小于1.0表示收縮  
                    值大于1.0表示放大
*/
//第五個參數pivotXType為動畫在X軸相對于物件位置類型  
//第六個參數pivotXValue為動畫相對于物件的X坐标的開始位置
//第七個參數pivotXType為動畫在Y軸相對于物件位置類型   
//第八個參數pivotYValue為動畫相對于物件的Y坐标的開始位置

scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(500);//設定動畫時間
heartImg.startAnimation(scaleAnimation);//啟動動畫
scaleAnimation.setAnimationListener(this);//設定監聽
           

3.TranslateAnimation(位置移動)

//第一個參數fromXDelta為動畫起始時 X坐标上的移動位置    
//第二個參數toXDelta為動畫結束時 X坐标上的移動位置      
//第三個參數fromYDelta為動畫起始時Y坐标上的移動位置     
//第四個參數toYDelta為動畫結束時Y坐标上的移動位置
translateAnimation = new TranslateAnimation(30, -80, 30, 300);
translateAnimation.setDuration(3000);//設定動畫持續時間
heartImg.startAnimation(translateAnimation);//啟動動畫
translateAnimation.setAnimationListener(this);//設定動畫監聽事件
           

4.RotateAnimation(旋轉動畫)

//第一個參數fromDegrees為動畫起始時的旋轉角度    
//第二個參數toDegrees為動畫旋轉到的角度   
//第三個參數pivotXType為動畫在X軸相對于物件位置類型  
//第四個參數pivotXValue為動畫相對于物件的X坐标的開始位置
//第五個參數pivotXType為動畫在Y軸相對于物件位置類型   
//第六個參數pivotYValue為動畫相對于物件的Y坐标的開始位置

rotateAnimation = new RotateAnimation(0, +350.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5F);
rotateAnimation.setDuration(3000);//設定動畫持續時間
heartImg.startAnimation(rotateAnimation);//啟動動畫
rotateAnimation.setAnimationListener(this);//設定動畫監聽
           

四:組合動畫

1.xml中組合動畫(這裡就隻寫一個簡單了)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="2000"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.4"
        android:toYScale="1.4" />
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>
           

2.javaCode中組合動畫,

主要使用到AnimationSet這個對象

animationSet = new AnimationSet(false);
                Animation alphAnimation = new AlphaAnimation(1.0f, 0.0f);
                Animation scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(2000);
                alphAnimation.setDuration(2000);
                animationSet.addAnimation(alphAnimation);
                animationSet.addAnimation(scaleAnimation);
                imageView.startAnimation(animationSet);
           

如有不對之處,還望各位大神指導,小女子在此謝過了!

繼續閱讀