今天說說ImageSwitcher和TextSwitcher這兩個類吧。我想還是主要來說說API DEMO裡這關於這兩個類的例子吧。
首先來看Demo中view包下面的ImageSwitcher1.java,這個DEMO實現的效果就是在Activity下面有一個小的圖片索引列表,用滑鼠點擊列表中任意一個圖片便可以對該圖片進行顯示,在每次顯示圖片的時候總有一個漸層的動畫效果。首先來看下它的布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Gallery android:id="@+id/gallery" android:background="#55000000"
android:layout_width="fill_parent" android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical" android:spacing="16dp" />
</RelativeLayout>
在這個布局檔案裡,外層的大的RelativeLayout包含了兩個控制項:分別是ImageSwithcer和Gallery。ImageSwithcer是用來圖片顯示那塊地區的控制項,Gallery是來控制底下那個表徵圖索引清單索引用的。
在ImageSwitcher1.java中,requestWindowFeature(Window.FEATURE_NO_TITLE)是使得這個 Activity沒有titlebar,進而這個圖片顯示地區會增大。Gallery這個類和ListView這個類用起來差不多,也是要利用 setAdapter來進行資源的設定。這裡對BaseAdapter進行了封裝,這裡通過getView這個函數來返回要顯示的那個ImageView 的。如下所示:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(105,105));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(0,0,0,0);
imageView.setImageResource(mThumbIds[position]);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
getView方法裡動態產生一個ImageView,然後利用setLayoutParams ,setImageResource和setBackgroundResource分別設定要圖片大小,圖片源檔案和圖片背景。當圖片被顯示到當前螢幕的時候這個函數就會被自動回調來提供要顯示的ImageView。下面就來說說ImageSwitcher,在ImageSwitcher1中實現了 ViewSwitcher.ViewFactory這個介面,這個介面裡有個方法makeView,來看下這個方法:
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return i;
}
這個方法為ImageSwitcher返回了一個View。ImageSwitcher調用過程是這樣的,首先要有一個Factory為它提供一個View,然後ImageSwitcher就可以初始化各種資源了。
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
注意在使用一個ImageSwitcher之前,一定要調用setFactory方法,要不setImageResource這個方法會報null 指標異常。 ImageSwitcher的轉場效果就是由上面最後兩句實現的,這裡的setInAnimation是資源被讀入到這個ImageSwitcher的時候動畫,setOutAnimation是資源檔從這個ImageSwitcher裡消失的時候要實現的動畫效果,這裡所有的動畫都是從 android.R系統檔案裡讀取的。
ImageSwitcher就說到這裡,TextViewSwitcher和ImageSwitcher用法大同小異,下面介紹下怎樣自己寫Animation。
在view包中的Animation2這個類使用了一些自己寫的Animation,這些Animation也是一些xml檔案,下面便是一例:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>
Translate 這個標籤裡說明字型相對於控制項的高度由原來高度的100%變換到0%。Alpha是設定轉換時候的透明位,如果從ImageSwitcher裡消失的話透明位就從1.0到0.0,如果是進入時的動畫,那麼透明位就從0.0變換到1.0。這個Animation是豎向的,同樣fromXDelta和 toXDelta這個屬性可以進行橫向動畫的設定。大家可以寫些例子來看看這些Animation的各種效果。
原文:http://xiaoxixi615.blog.sohu.com/133612066.html