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

Android學習指南之十三:用Gallery打造畫廊滑動效果

編輯:關於 Android 開發

我們在智能手機上想必都見過那種可以左右滑動的圖片集,這種效果在Android裡正是靠Gallery來實現的。Gallery是一種內部子元素能夠水平滾動、並會自動把當前選中項居中定位的布局組件。本篇就來動手做一個Gallery畫廊範例。

先上一張運行效果圖,讓大家有個直觀的認識。

下面直接上代碼,關鍵解釋都放在註釋裡。

第一步,新建項目HelloGallery。

第二步,把wallpaper_0.jpg到wallpaper_9.jpg共10張圖片拷貝到res/drawable目錄。

第三步,編寫res/layout/main.xml。這裡用FrameLayout實現圖層疊加:底層的ImageView顯示大圖,上層的Gallery展示可滑動的縮略畫廊。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01">
  3. <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/ImageView01" android:src="@drawable/wallpaper_0"></imageview>
  4. <gallery android:layout_height="wrap_content" android:layout_width="fill_parent" xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:spacing="5dp"></gallery>
  5. </framelayout>

其中android:spacing="5dp"用來設定縮略圖之間的間距。

第四步,在res/values/目錄新建attrs.xml,聲明一個可樣式化屬性,供Gallery子項背景使用。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3.     <declare-styleable name="HelloGallery">
  4.         <attr name="android:galleryItemBackground"></attr>
  5.     </declare-styleable>
  6. </resources>

第五步,編寫MainHelloGallery.java。核心在於自定義一個繼承BaseAdapter的ImageAdapter,負責生成每個縮略圖視圖;同時為Gallery設置OnItemClickListener,點擊後把選中圖片顯示到背景的大ImageView上。

  1. package android.basic.lesson13;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AdapterView;
  9. import android.widget.AdapterView.OnItemClickListener;
  10. import android.widget.BaseAdapter;
  11. import android.widget.Gallery;
  12. import android.widget.ImageView;
  13. import android.widget.Toast;
  14. public class MainHelloGallery extends Activity {
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.         final ImageView iv=(ImageView)findViewById(R.id.ImageView01);
  20.         Gallery g=(Gallery)findViewById(R.id.Gallery01);
  21.         //為Gallery設置圖片適配器
  22.         g.setAdapter(new ImageAdapter(this));
  23.         //Gallery是AdapterView子類,設置項點擊監聽
  24.         g.setOnItemClickListener(new OnItemClickListener() {
  25.             @Override
  26.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  27.                 //提示點擊的是第幾張
  28.                 Toast.makeText(MainHelloGallery.this, "" + position, Toast.LENGTH_LONG).show();
  29.                 //把選中項顯示到背景大圖
  30.                 iv.setImageResource(((ImageView)view).getId());
  31.             }
  32.         });
  33.     }
  34.     //自定義繼承BaseAdapter的圖片適配器
  35.     public class ImageAdapter extends BaseAdapter {
  36.         int mGalleryItemBackground;
  37.         private Context mContext;
  38.         private Integer[] mImageIds = { R.drawable.wallpaper_0, R.drawable.wallpaper_1, R.drawable.wallpaper_2, R.drawable.wallpaper_3, R.drawable.wallpaper_4, R.drawable.wallpaper_5, R.drawable.wallpaper_6, R.drawable.wallpaper_7, R.drawable.wallpaper_8, R.drawable.wallpaper_9 };
  39.         public ImageAdapter(Context c){
  40.             mContext = c;
  41.             //讀取styleable資源
  42.             TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
  43.             mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
  44.             a.recycle();
  45.         }
  46.         @Override
  47.         public int getCount() {
  48.             return mImageIds.length;
  49.         }
  50.         @Override
  51.         public Object getItem(int position) {
  52.             return position;
  53.         }
  54.         @Override
  55.         public long getItemId(int position) {
  56.             return position;
  57.         }
  58.         @Override
  59.         public View getView(int position, View convertView, ViewGroup parent) {
  60.             ImageView iv = new ImageView(mContext);
  61.             iv.setImageResource(mImageIds[position]);
  62.             //給ImageView設置Id,否則默認都是-1
  63.             iv.setId(mImageIds[position]);
  64.             iv.setLayoutParams(new Gallery.LayoutParams(120, 160));
  65.             iv.setScaleType(ImageView.ScaleType.FIT_XY);
  66.             iv.setBackgroundResource(mGalleryItemBackground);
  67.             return iv;
  68.         }
  69.     }
  70. }

點擊某張縮略圖時,該子元素的圖片就會顯示到背後那層ImageView上。感興趣的同學可以再了解一下AdapterView的繼承體系:

掌握了Gallery的用法,你就能輕鬆做出炫酷的圖片滑動效果了。

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