6.1.6 Gallery結合案例詳解
Gallery(相冊)控件是個很不錯的圖片查看控件,屏幕中有一個圖片列表,Gallery類的繼承關係如下:java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<T extends android.widget.Adapter>
↳ android.widget.AbsSpinner
↳ android.widget.Gallery
這個Gallery案例,可以用手滑動Gallery,當用戶點擊某個圖片彈出一個Toast,如6-11圖:

6-11 Gallery控件使用效果圖
程序代碼請參考代碼清單6-9:
【代碼清單6-9】 chapter6_5/src/com/work/GalleryActivity.java
public class GalleryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(g);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(R.string.gallerytext);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
return true;
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
private Context mContext;
private Integer[] mImageIds = {
R.drawable.beijing,
R.drawable.changsha,
R.drawable.chengdu,
R.drawable.chongqing,
R.drawable.haerbing,
R.drawable.jinan,
R.drawable.jiujiang,
R.drawable.kunming,
R.drawable.nanjing
};
}
}
代碼的關鍵的地方是實現BaseAdapter適配器類——ImageAdapter,其中關鍵是getView()實現。在convertView為null時候實例化控件,imageView.setLayoutParams(new GridView.LayoutParams(136, 88)是設置一個單元格中圖片的大小是136×88像素。imageView.setScaleType(ImageView.ScaleType.FIT_XY) 縮放圖片使用FILL方式。imageView.setImageResource(mImageIds[position])為圖片控件設置圖片。
在布局文件/chapter6_5/res/layout/main.xml中添加Gallery控件:
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:andro android:
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
本例中設置圖片的背景樣式是邊框樣式,如圖6-12。

圖6-12 圖片背景樣式
imageView.setBackgroundResource(mGalleryItemBackground)語句就是設定樣式的,成員變量mGalleryItemBackground是在ImageAdapter的構造方法中初始化的。
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
mGalleryItemBackground是與galleryItemBackground背景資源綁定的id值,這個id對應的galleryItemBackground屬性就是設定帶有邊框的背景樣式。
此外還要在chapter6_5/res/values/目錄下麵創建一個attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
這是一個自定義控件屬性的xml文件。但是在Android1.0時候沒有這麼麻煩,而是如下方式實現:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
android.R.styleable.Theme_galleryItemBackground, 0);
a.recycle();
}
android.R.styleable.Theme_galleryItemBackground 屬性在Android1.0是可以訪問的,而在Android1.0之後就不能訪問了,而要通過本例的方式獲得galleryItemBackground的id值。
出自《Android開發案例驅動教程》
最後更新:2017-04-02 06:51:45