編輯:Android 開發實例
Gallery 稱得上是早期 Android 內建的「看圖神器」:它把一堆圖片排成可橫向滑動的膠卷,開發者只要提供一個 Adapter,捲動、對齊與選取的行為都由元件本身處理,成品視覺也相當體面。
整個流程只有三件事:在版面配置中放一個 Gallery、寫一個繼承 BaseAdapter 的資料提供者、然後把 Adapter 掛上去。Gallery 自己不存資料,所有內容都靠 Adapter 的 getView() 決定。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Gallery
android:id="@+id/Gallery01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Gallery>
</LinearLayout>
MainActivity 只負責取得 Gallery 實例並設定 Adapter;真正的重活在 MyAdapter 裡。
package com.amaker.test;
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 MainActivity extends Activity {
@Override
// 建立時被呼叫
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.Gallery01);
g.setAdapter(new MyAdapter(this));
}
class MyAdapter extends BaseAdapter {
Context context;
MyAdapter(Context context) {
this.context = context;
}
private Integer[] imgs = {
R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7, R.drawable.gallery_photo_8,
R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7, R.drawable.gallery_photo_8
};
public int getCount() {
return imgs.length;
}
public Object getItem(int item) {
return item;
}
public long getItemId(int id) {
return id;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imgs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
return imageView;
}
}
}
範例刻意把八張圖重複一次,是為了讓滑動效果在兩端看起來更連續,這是常見的展示技巧,正式專案中通常會改成讀取 MediaStore 或網路清單。
getView() 每次都 new 一個 ImageView,在 Gallery 這種需要頻繁重繪的元件上會造成不必要的配置與回收。若要優化,可以沿用 Gallery 提供的 convertView 參數(Gallery 本身沒有像 ListView 那樣廣泛複用,但仍可自行維護一份 View 快取),並把圖片載入交給背景執行緒與快取機制處理,避免大圖造成卡頓。
另外,Gallery 在新版 Android 已被標記為不建議使用,若要在新專案中實作同類效果,通常會改用 RecyclerView 搭配水平的 LayoutManager。
Android Intent 啟動其他應用的實現方法
Intent 除了可以在同一個應用內切換 Activity,也可以用來啟動其他應用。最常見的方式是透過 ComponentName 明確指定目標應用的套件名稱與啟動 Ac
Android 客戶端透過 Socket 與服務器通信的實現方法
本文通過一個完整 Demo,講解 Android 客戶端如何透過 Socket 與服務器通信。 由於 Android 可以完全使用 java.io.* 和 java.ne
Android JSON 解析實作:用 JSONObject 取得天氣資料
從網路取得 JSON在 Android 應用中,JSON 常用來承載 API 回傳的資料。解析時,通常先取得完整字串,再建立 JSONObject,逐一讀取需要的欄位。解
Android JSON 解析入門:用 JSONObject 抓出你要的那幾個欄位
JSON 是 JavaScript Object Notation 的縮寫,做為一種與語言無關的資料交換格式,這些年在行動端幾乎全面取代了 XML:它更輕、更好讀,解析也