Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 中階開發

Android 基礎教學:打造媲美 iPhone 的 Gallery 拖動相片特效

編輯:中階開發

Gallery 是 Android 早期相當經典的橫向捲動相簿元件,滑動時相片會居中放大、兩側遞減,視覺效果與 iPhone 的相片瀏覽有異曲同工之妙。本文就帶大家一步步實作這個特效。

Step 1:準備圖片素材

將 icon2、icon3、icon4、icon5、icon6 五張圖片匯入 res/drawable,加上專案本身自帶的 icon.png,總共會有六張圖片可供展示。

Step 2:建立 Android 專案

新增一個 Android 專案,命名為 GalleryDemo。

Step 3:設計 UI 版面

在 main.xml 中,以垂直方向的 LinearLayout 為外層容器,上方擺放一個用來顯示文字標題的 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:撰寫主程式 GalleryDemo.java

核心觀念在於自訂一個繼承自 BaseAdapter 的 ImageAdapter,負責把 drawable 中的圖片資源轉換成 Gallery 可顯示的 View。其中 getView 會為每個位置建立一個 ImageView,並透過 getScale 依據與中央的距離回傳縮放比例,做出「越靠中間越大」的效果:

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 {
    /** Called when the activity is first created. */
    @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 };

        /* 構造器只有一個參數,即要儲存的 Context */
        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,傳入陣列 ID 值使之讀取與成像 */
        public View getVIEw(int position, View convertView, VIEwGroup parent) {
            /* 建立一個 ImageVIEw 物件 */
            ImageVIEw i = new ImageVIEw(this.myContext);
            i.setImageResource(this.myImageIds[position]);
            i.setScaleType(ImageVIEw.ScaleType.FIT_XY);
            /* 設置這個 ImageVIEw 的寬高,單位為 dip */
            i.setLayoutParams(new Gallery.LayoutParams(120, 120));

            /* 依據距離中央的位移量,利用 getScale 回傳 View 的大小 (0.0f to 1.0f) */
            i.setScale(Math.max(0.0f, getScale(false, position - getSelection())));
            return i;
        }

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

        private int getSelection() {
            return ((Gallery) findVIEwById(R.id.myGallery1)).getSelectedItemPosition();
        }
    }
}

Step 5:執行成果

完成上述步驟後執行專案,即可看到相片在滑動時居中的圖片放大、兩側遞減縮小的漂亮效果:

補充說明:本範例的程式碼大致參照 Android SDK 內附的開發範例改寫而成,主要目的是幫助入門者理解 Gallery 與自訂 Adapter 的運作原理。掌握縮放比例的计算後,你也能自由調整動畫的手感與節奏。

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