編輯:關於 Android 程式設計
本篇筆記整理一個圖片瀏覽範例的關鍵程式碼,涵蓋自定義 Adapter(ImageAdapter、ImgAdapter)以及在 MainActivity 中整合 Gallery、ImageSwitcher、GridView、ListView、Spinner 等多種控件的做法。
[java]
package com.example.prjandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class FullImageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_img1);
Intent intent = getIntent();
int position = intent.getExtras().getInt("id");
ImageAdapter imgAdapter = new ImageAdapter(FullImageActivity.this);
ImageView imgView = (ImageView) findViewById(R.id.imgView1);
imgView.setImageResource(imgAdapter.mThumbIds[position]);
}
}
[java]
package com.example.prjandroid;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.emacs1, R.drawable.emacs2, R.drawable.emacs3,
R.drawable.emacs4, R.drawable.emacs5, R.drawable.emacs6,
R.drawable.emacs7, R.drawable.emacs8, R.drawable.emacs9,
R.drawable.emacs10, R.drawable.emacs11, R.drawable.emacs12,
R.drawable.emacs13, R.drawable.emacs14, R.drawable.emacs15
};
public ImageAdapter(Context context) { mContext = context; }
@Override public int getCount() { return mThumbIds.length; }
@Override public Object getItem(int position) { return position; }
@Override public long getItemId(int position) { return 0; }
@Override
public View getView(int position, View converView, ViewGroup parent) {
ImageView imgView = new ImageView(mContext);
imgView.setImageResource(mThumbIds[position]);
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imgView;
}
}
與 ImageAdapter 類似,差別在於 Gallery 版會從自定義樣式屬性中取得項目背景,並以 Gallery.LayoutParams 設定圖片尺寸:
[java]
public class ImgAdapter extends BaseAdapter {
private int[] resPics = new int[] {
R.drawable.emacs1, R.drawable.emacs2, R.drawable.emacs3,
R.drawable.emacs4, R.drawable.emacs5, R.drawable.emacs6,
R.drawable.emacs7, R.drawable.emacs8, R.drawable.emacs9,
R.drawable.emacs10
};
private Context m_context;
private int m_galleryItemBackGround;
public ImgAdapter(Context context) {
m_context = context;
TypedArray typeArray = m_context.obtainStyledAttributes(R.styleable.galleryThem);
m_galleryItemBackGround = typeArray.getResourceId(
R.styleable.galleryThem_android_galleryItemBackground, 0);
}
@Override public int getCount() { return resPics.length; }
@Override public Object getItem(int position) { return position; }
@Override public long getItemId(int position) { return position; }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(m_context);
imgView.setImageResource(resPics[position]);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setLayoutParams(new Gallery.LayoutParams(163, 106));
imgView.setBackgroundResource(m_galleryItemBackGround);
return imgView;
}
}
MainActivity 透過 LayoutInflater 預先 inflate 多個布局(Gallery、ImageSwitcher、GridView、ListView、AutoComplete、Base View 等),並用一組 boolean 旗標記錄各頁面是否已初始化,避免重複配置。它同時實作了 OnClickListener、OnItemSelectedListener、OnItemClickListener 等多個介面,以處理不同控件的互動。以下節錄幾個關鍵片段:
[java]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
m_view1 = inflater.inflate(R.layout.activity_main, null);
m_view2 = inflater.inflate(R.layout.view_gallery, null);
m_view3 = inflater.inflate(R.layout.view_image_switcher, null);
m_view4 = inflater.inflate(R.layout.gridview02, null);
setView(m_view1);
// 各按鈕設定 OnClickListener……
}
// 以 GridView 為例:點擊項目後跳轉到大圖頁並傳遞位置
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent = new Intent(getApplicationContext(), FullImageActivity.class);
intent.putExtra("id", position);
startActivity(intent);
}
// ImageSwitcher 需要實作 ViewFactory 來產生 view
@Override
public View makeView() {
ImageView imgView = new ImageView(this);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imgView;
}
這個範例的價值在於把多個常見控件的用法串在一起:用 BaseAdapter 的兩個變體分別驅動 GridView 與 Gallery;用 ImageSwitcher 搭配 ViewFactory 實現圖片翻轉動畫;用 Intent 在頁面間傳遞資料完成大圖瀏覽。透過旗標延遲初始化的技巧,也能有效降低不必要的資源配置。
Android 視頻播放器實作:豎屏播放界面
豎屏播放界面 播放視頻通常放在獨立 Activity。豎屏時,上半部播放影片,下半部顯示標題、解析度、檔案大小與建立時間等資訊。 啟動播放器 點擊列表項目時,可建立 I
Android 拍照與相簿選圖後裁剪頭像的完整做法
先看成果,如果這正是你想要的效果,那這段程式碼就可以直接參考;實際套用時記得依需求靈活調整。 布局檔 <LinearLayout xmlns:android=
Android MediaPlayer 的生命週期與狀態轉換全解析
MediaPlayer 的狀態轉換圖,其實就是它的生命週期圖。把這張圖吃透,寫出來的播放程式才會考慮得夠周全、夠有健壯性。 下面用幾張圖,一步步把整個生命週期演一遍: 圖
Android 手把手教你繪製圓形頭像
自從騰訊 QQ 的圓形頭像流行開來,越來越多應用的個人主頁或資料面板也開始採用圓形頭像,它讓整體布局顯得更加優雅而得體。本文就來動手實現一個圓形(圓角)頭像。 第一步,創