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

仿 iPhone 相片流:Gallery 拖動切換特效實作

編輯:關於 Android 程式設計

Gallery 雖然在後續版本中被標記為過時,但它示範的「自定義 Adapter 驅動橫向滾動列表」思路,至今仍是理解 Android 列表機制的好教材。本文用它做一個可拖動切換的相片流。

Step 1:準備素材

把 icon2 到 icon6 五張圖片放進 res/drawable,加上 icon.png 本身共六張。

Step 2:建立工程

新建 Android 項目,命名為 GalleryDemo。

Step 3:編寫佈局

頂部一個 TextView 用來回顯當前選中項,下方放 Gallery:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:background="@drawable/white"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/myTextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:gravity="center_vertical|center_horizontal" />

    <Gallery
        android:id="@+id/myGallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom" />
</LinearLayout>

Step 4:自定義 ImageAdapter

核心是繼承 BaseAdapter 並在 getView 中為每個位置建立 ImageView。這裡另外覆寫了 getScale,用 1/(2^offset) 的公式讓離中心越遠的項縮得越小,做出透視般的層次感。

package com.android.test;

import com.android.test.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((Gallery) findViewById(R.id.myGallery1)).setAdapter(new ImageAdapter(this));
    }

    public class ImageAdapter extends BaseAdapter {
        /* myContext 為 Context 父類 */
        private Context myContext;

        /* 使用 res/drawable 中的圖片作為來源 */
        private int[] myImageIds = {drawable.icon, drawable.icon2, drawable.icon3,
                drawable.icon4, drawable.icon5, drawable.icon6};

        public ImageAdapter(Context c) {
            this.myContext = c;
        }

        /* 返回圖片總數量 */
        public int getCount() {
            return this.myImageIds.length;
        }

        /* 取得目前容器中圖像的數組 ID */
        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        /* 取得目前欲顯示的圖像 View */
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);
            i.setImageResource(this.myImageIds[position]);
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            /* 設置這個 ImageView 的寬高,單位為像素 */
            i.setLayoutParams(new Gallery.LayoutParams(120, 120));
            return i;
        }

        /* 依據距離中央的位移量返回縮放比例(0.0f 到 1.0f) */
        public float getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
            return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
        }
    }
}

Step 5:運行

現代專案該怎麼替換

Gallery 在 API 14 起已被標記為 deprecated,正式專案中建議改用 HorizontalScrollView + LinearLayout,或直接上 RecyclerView 搭配 LinearLayoutManager(HORIZONTAL)。本示例的價值在於示範了 Adapter 如何驅動滾動容器,這套思路換到 RecyclerView 上完全適用——只需把 getView 換成 onBindViewHolder,並自行實作居中放大效果即可。

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