天天看點

Drawable解析4——StateListDrawable和AnimationDrawable

1、估計StateListDrawable是大家用的最多一個drawable了,所有的控件背景基本上都使用了StateListDrawable,以實作其在不同狀态下顯示不同的效果,例如按鈕的按下、選中、預設、禁用等多種模式狀态。StateListDrawable用于管理一組drawable,每個drawable都對應一組狀态,狀态的選擇類似于java中的switch-case組合,按照順序比較狀态,當遇到比對的的狀态,就傳回對應的drawable,是以需要把最精确的比對放置到最前面,整體按照從精确到粗略的順序排列。在xml檔案中使用selector作為根節點來定義StateListDrawable,并使用item作為子節點定義不同狀态下的drawable。selector和item的屬性如下:

  • 定義一個state_list_drawable.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="

http://schemas.android.com/apk/res/android

">

    <item android:drawable="@drawable/button_normal" android:state_focused="false" android:state_pressed="false"/>

    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>

    <item android:drawable="@drawable/button_focused" android:state_focused="true"/>

</selector>

  • 當然我也可以不使用drawable,直接使用顔色也可以,編寫state_list_color.xml

   <item android:state_focused="false" android:color="#ffffff"/>

    <item android:state_focused="true" android:color="#ff0000"/>

  • 在activity_main.xml中為一個Button和EditText分别設定背景

 <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/state_list_drawable"

        android:text="按鍵效果" />

    <EditText

        android:layout_width="match_parent"

        android:textColor="@drawable/state_list_color" >

    </EditText>

2、AnimationDrawable代表一個動畫,既支援逐幀動畫又支援補間動畫(補間動畫已經屬于動畫的範疇了,但既然他也是Drawable,那就說一下吧)。先說下幀動畫的使用方法,在xml檔案中使用animation-list作為根節點定義AnimationDrawable,使用Item設定進行播放發每一幀所需的Drawable資源。可以使用android:oneshot屬性設定是否循環播放。

寫個例子:

<?xml version="1.0" encoding="utf-8"?>      
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"      
android:oneshot="false">      
<itemandroid:drawable="@drawable/anim1"      
android:duration="300"/>      
<itemandroid:drawable="@drawable/anim2"      
android:duration="300"/>      
<itemandroid:drawable="@drawable/anim3"      
android:duration="300"/>      

</animation-list>

定義了AnimationDrawable之後,需要主動調用AnimationDrawable的start方法播放動畫,注意在onCreate方法中調用start方法是沒有任何效果的,這是因為此時View還沒有完成初始化,可以使用handler來實作延遲播放動畫,代碼如下:

 mHandler.postDelayed(new Runnable() {

                        public void run() {

                            (mAnimationDrawable.start();

                        }

                    }, 300);

再說下補間動畫的使用, 補間動畫使用set作為根節點進行定義,結構如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@[package:]anim/interpolator_resource"

android:shareInterpolator=["true"|"false"]

android:duration="持續時間">

<alpha/>設定透明度的改變

<scale/>設定圖檔進行縮放改變

<translate/>設定圖檔進行位移變換

<rotate/>設定圖檔進行旋轉

舉個例子,在res\anim\my_anim.xml定義

<?xml version="1.0" encoding="UTF-8"?>

<set xmlns:android="

"

    android:interpolator="@android:anim/linear_interpolator"

    android:duration="5000">

    <!-- 定義縮放變換 -->

    <scale android:fromXScale="1.0"  

        android:toXScale="1.4"  

        android:fromYScale="1.0"  

        android:toYScale="0.6"  

        android:pivotX="50%" 

        android:pivotY="50%" 

        android:fillAfter="true" 

        android:duration="2000"

    /> 

    <!-- 定義位移變換 -->        

    <translate android:fromXDelta="10"

        android:toXDelta="130"

        android:fromYDelta="30"

        android:toYDelta="-80"

    />

</set>

在java代碼中如下調用

mImageView2 = (ImageView) findViewById(R.id.imageView2);

        mAnimationDrawable2=AnimationUtils.loadAnimation(this, R.anim.my_anim);

        // 設定動畫結束後保留結束狀态

        mAnimationDrawable2.setFillAfter(true);

        mButton1.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                mImageView2.startAnimation(mAnimationDrawable2);

            }

        });

看下最終效果吧:

繼續閱讀