Android 筆記之 視圖動畫(Animation)和幀動畫

視圖動畫(Animation)漸變動畫,針對View的動畫,主要支持平移、旋轉、縮放、透明度
通過 View 不斷的重繪實現動畫
優點:使用方便,效率較高
缺點:不具備交互性,其響應事件的位置依然在動畫前的地方
分類:
  • 透明度(AlphaAnimation
  • 旋轉(RotateAnimation
  • 縮放(ScaleAnimation
  • 位移(TranslateAnimation
  • 動畫集合(AnimationSet)
    均是 Animation 的子類,即可以通過XML 來定義,也可以通過代碼來創建
1、透明度動畫

             爲視圖增加透明度變換的動畫

 AlphaAnimation aa = new AlphaAnimation(0, 1);//透明度變化範圍
 aa.setDuration(1000);
 view.startAnimation(aa);
2、旋轉動畫
            爲視圖增加旋轉變換的動畫
        RotateAnimation ra = new RotateAnimation(0, 360, //起始角度及最終角度
                                                 100, 100);//旋轉中心點的座標
        ra.setDuration(1000);
        view.startAnimation(ra);

        RotateAnimation ra = new RotateAnimation(0,360,
                            RotateAnimation.RELATIVE_TO_SELF,//設置參考系爲自身中心點,也可設置爲父容器爲參考系
                            RotateAnimation.RELATIVE_TO_SELF);

3、縮放動畫
            爲視圖增加縮放變換的動畫
       ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);//縮放的起始座標及最終座標
       ScaleAnimation sa =  new ScaleAnimation(0,1,0,1,
                           ScaleAnimation.RELATIVE_TO_SELF,//設置以自身中心爲縮放中心
                           ScaleAnimation.RELATIVE_TO_SELF);
        sa.setDuration(1000);
        view.startAnimation(sa);

4、位移動畫
            爲視圖增加位移變換的動畫
        TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);//位移的起始座標及最終座標
        ta.setDuration(1000);
        view.startAnimation(ta);

5、動畫集合
            AnimationSet 動畫合集
        AnimationSet as = new AnimationSet(true);
        as.setDuration(1000);
        
        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setDuration(1000);
        as.addAnimation(aa);

        RotateAnimation ra = new RotateAnimation(0, 360, 100, 100);
        ra.setDuration(1000);
        as.addAnimation(ra);
        
        TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
        ta.setDuration(1000);
        as.addAnimation(ta);
        
        ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
        sa.setDuration(1000);
        as.addAnimation(sa);
        view.startAnimation(as);
        as.setAnimationListener(new Animation.AnimationListener() {//監聽動畫事件
            @Override
            public void onAnimationStart(Animation animation) {//開始
                
            }

            @Override
            public void onAnimationEnd(Animation animation) {//結束

            }

            @Override
            public void onAnimationRepeat(Animation animation) {//重複

            }
        });

這四種動畫實現方式都是通過Animation類和AnimationUtils配合實現。
也可以通過xml實現:動畫的XML文件在工程中res/anim目錄。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@[package:] anim/interpolator_resource"
     android:shareInterpolator="true">
    
    <!--透明度動畫
    fromAlpha 透明度起始值,比如0.1
    toAlpha   透明度結束值,比如1
    -->
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float"/>
    
    
    <!--縮放動畫
    fromXScale 水平縮放的起始值,比如0.5
    toXScale   水平縮放的結束值,比如1.2
    pivotX/pivotY 中縮放心座標點的x,y座標
    -->
    <scale
        android:fromXScale="float"
        android:fromYScale="float"
        android:toXScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float"/>
    
    <!--平移動畫
    fromXDelta x的起始值,比如0
    toXDelta   x的結束值,比如100
    -->
    <translate
        android:fromXDelta="float"
        android:fromYDelta="float"
        android:toXDelta="float"
        android:toYDelta="float"/>
    
    <!--旋轉動畫
    fromDegrees 旋轉開始的角度,比如0
    toDegrees   旋轉結束的角度,比如180
    pivotX/pivotY 中心座標點
    -->
    <rotate
        android:fromDegrees="float"
        android:pivotX="float"
        android:pivotY="float"
        android:toDegrees="float"/>
    <set>
        ....
    </set>
</set>

從上可以看出,View 的視圖動畫既可以是單個動畫,也可以是一系列動畫的組合<set> 標籤表示動畫集合,對應AnimationSet 類,它可以包含若干個動畫,並且他的內部也是可以嵌套其它動畫集合的,他的兩個屬性含義如下:
interpolator:
    表示動畫集合所採用的插值器,插值器影響動畫的速度,比如非勻速動畫就需要通過插值器來控制動畫的播放過程。這個屬性
可以不指定,默認爲@android:anim/accelerate_decelerate_interpolator,即加速減速插值器

shareInterpolator:表示集合中的動畫是否和集合共享一個插值器,如果不指定插值器,則子動畫就需要單獨指定所需的插值器或者使用默認值。
    除了上述的屬性外,View 動畫還有一些常用的屬性,如下
  • android:duration        動畫的持續時間
  • android:fillAfter      動畫結束後View 是否停留在結束位置,true表示 View 停留在結束位置,false則不停留
例如:rotate.xml
<?xml version="1.0" encoding="utf-8"?>  
  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:fillAfter = "false"  
    android:zAdjustment="bottom"  
    >  
    <rotate  
        android:fromDegrees="0"  
        android:toDegrees="360"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:duration="4000"  
        />  
</set>  

     如何應用上面的動畫呢?如下所示:

Button mButton = (Button)findViewById(R。id.button);
Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation_test);
mButton.startAnimation(animation);

Frame Animation(幀動畫)主要是設置View的背景,可以以動畫的形式爲View設置多張背景
    幀動畫是順序播放事先做好的圖像,跟電影類似。Android SDK提供了另外一個類AnimationDrawable來定義使用幀動畫
利用xml文件實現:res/drawable-hdpi/frame.xml:
<?xml version="1.0" encoding="utf-8"?>  
  
<animation-list  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:oneshot="true"  
  >  
       <item android:drawable="@drawable/p1" android:duration="1000"></item>  
       <item android:drawable="@drawable/p2" android:duration="1000"></item>  
       <item android:drawable="@drawable/p3" android:duration="1000"></item>  
       <item android:drawable="@drawable/p4" android:duration="1000"></item>  
       <item android:drawable="@drawable/p5" android:duration="1000"></item>  
       <item android:drawable="@drawable/p6" android:duration="1000"></item>  
</animation-list> 

使用動畫
AnimationDrawable anim = (AnimationDrawable)getResources().  
getDrawable(R.drawable.frame);  
textWidget = (TextView)findViewById(R.id.text_widget);  
textWidget.setText("背景漸變動畫效果");  
textWidget.setBackgroundDrawable(anim);  
anim.start();  

    這裏有點不同的是,利用AnimationDrawable實現動畫時,本身並沒有提供接口來監聽動畫的狀態(開始,結束),需要自己處理。幀動畫使用比較簡單,但是比較容易引起OOM,所以在使用幀動畫時應儘量避免使用過多尺寸較大的圖片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章