Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖像顯示(十四):用 ImageSwitcher 加上切換動畫

編輯:關於 Android 程式設計

上一節我們用 Gallery 搭配 ImageView 來展示圖片。但有時候,直接讓一張圖「啪」地出現在 ImageView 裡,視覺上顯得有點突兀。如果你希望切換圖片時能有一點過場動畫,這時候就該請出 ImageSwitcher 了。它天生就是為「帶動畫的圖片切換」而設計的。

版面配置

把 Gallery 與 ImageSwitcher 一起放進版面:Gallery 負責陳列縮圖,ImageSwitcher 則負責大圖的切換顯示。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageSwitcher
        android:id="@+id/switcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

另外別忘了在 res/values 下新增一個 attrs.xml,宣告 Gallery 的樣式屬性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>

關鍵:實作 ViewFactory

使用 ImageSwitcher 最重要的一步,是讓 Activity 實作 ViewFactory 介面。這個介面為 ImageSwitcher 產生要顯示的 view,我們只需實作 makeView(),回傳一個設定好外觀的 ImageView:

public class ImageSwitcherActivity extends Activity implements ViewFactory {

    Integer[] imageIDs = {
        R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
        R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7
    };

    private ImageSwitcher imageSwitcher;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
        imageSwitcher.setFactory(this);

        // 設定左右滑動的切換動畫
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
                this, android.R.anim.slide_in_left));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
                this, android.R.anim.slide_out_right));

        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                imageSwitcher.setImageResource(imageIDs[position]);
            }
        });
    }

    public View makeView() {
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return imageView;
    }
}

切換動畫的設定

ImageSwitcher 的動畫透過 setInAnimation 與 setOutAnimation 設定。除了本例使用的左右滑動(slide_in_left / slide_out_right),系統也提供了淡入淡出效果可以替換:

imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
        this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
        this, android.R.anim.fade_out));

至於 ImageAdapter 的實作,則與上一節 Gallery 的範例幾乎相同,負責把 Gallery 與圖片資源綁定。差別在於:Gallery 中的縮圖由 Adapter 產生,而大圖的顯示則交給 ImageSwitcher 透過 makeView() 生成的 view 來負責。

完成後按 F11 在模擬器上執行,點選 Gallery 中的縮圖,上方的大圖就會帶著滑動動畫切換呈現,過場效果自然順滑許多。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved