編輯:關於 Android 程式設計
Gallery 雖然在後續版本中被標記為過時,但它示範的「自定義 Adapter 驅動橫向滾動列表」思路,至今仍是理解 Android 列表機制的好教材。本文用它做一個可拖動切換的相片流。
把 icon2 到 icon6 五張圖片放進 res/drawable,加上 icon.png 本身共六張。
新建 Android 項目,命名為 GalleryDemo。
頂部一個 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>
核心是繼承 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)));
}
}
}


Gallery 在 API 14 起已被標記為 deprecated,正式專案中建議改用 HorizontalScrollView + LinearLayout,或直接上 RecyclerView 搭配 LinearLayoutManager(HORIZONTAL)。本示例的價值在於示範了 Adapter 如何驅動滾動容器,這套思路換到 RecyclerView 上完全適用——只需把 getView 換成 onBindViewHolder,並自行實作居中放大效果即可。
Android 報警功能實作:鈴音、震動與狀態切換
需求說明 在某些應用场景中,例如緊急救援、保全或工業巡檢,需要加入報警功能。使用者點擊報警圖示後,應用 should 立即播放鈴音並啟動震動,同時透過畫面狀態切換提示目前
Android ORM 框架之 greenDAO 應用基礎
greenDAO 簡介greenDAO 是 Android 上常見的 ORM 框架,具有高效能、使用簡潔與可加密等特色。greenDAO 3 提供兩種使用方式:Java
20 行打造 Android CircleImageView:PorterDuffXfermode 圓形頭像
圓形頭像不一定需要依賴第三方庫。自訂 ImageView,在 onDraw 中利用 PorterDuffXfermode 的 DST_IN 模式,就能把方形圖片裁切成圓形
Android 監聽應用安裝與卸載廣播實作教學
BroadcastReceiver 的基本觀念 BroadcastReceiver 是系統全域的廣播監聽類,核心方法是 onReceive()。自訂的接收器繼承它並實作自