Android 畫廊控件Gallary。将圖檔顯示成連續的帶狀。
package com.gallerydemo;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class MainActivity extends Activity {
/*************************************
* 畫廊控件的使用Gallery
*
* 1.Gallery 顯示的是一個水準的清單選擇框允許使用者通過拖動來顯示上一個下一個清單項 2.使用步驟:a:定義布局檔案 b:定義資料擴充卡類
* c:關聯Gallery控件和資料擴充卡 d:為Gallery每個item添加事件。
* 3.由于使用大量圖檔可能出現記憶體溢出,API16已經廢棄,HorizontableScroolView和ViewPager
*
************************************/
private Gallery gallery; // 在API 16中已經不再支援
private String TAG = "GalleryDemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
// 關聯Gallery和資料擴充卡
try {
gallery.setAdapter(new ImageAdapter(this));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "====5");
gallery.setOnItemClickListener(new OnItemClickListener() {
// @Override
/*
* parent:指向資料擴充卡的指針 v:對應于item view的句柄
* position:item在資料擴充卡adapter中的位置 id:item在資料擴充卡的第幾行
*/
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// TODO Auto-generated method stub
MainActivity.this.setTitle(String.valueOf(position));
Log.i(TAG, "position: " + position + "id: " + id);
}
});
}
/*
* BaseAdapter就Android應用程式中經常用到的基礎資料擴充卡,它的主要用途是将一組資料傳到像ListView、Spinner、
* Gallery及GridView等UI顯示元件,它是繼承自接口類Adapter, 有關BaseAdapter
*
*/
// 為Gallery提供資料源
private class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Integer> imgList = new ArrayList<Integer>();
private ArrayList<Object> imgSize = new ArrayList<Object>();
// 建立與資源之間的關聯
public ImageAdapter(Context context) throws IllegalArgumentException,
IllegalAccessException {
mContext = context;
// 利用反射機制來擷取資源中的圖檔ID和尺寸
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields) {
if (!"ic_launcher".equals(field.getName()))// 除了icon之外的圖檔
{
int index = field.getInt(R.drawable.class);
imgList.add(index);// 這裡把int 轉換成了Integer
int size[] = new int[2];
Bitmap bmImg = BitmapFactory.decodeResource(getResources(),
index);
size[0] = bmImg.getWidth();
size[1] = bmImg.getHeight();
imgSize.add(size);
}
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// int是JAVA的一個基本類型,而Integer是JAVA的一個類,對應
// int。因為在某些地方不可以用int而要用Integer。而且基本類型運算的速度也要快。
ImageView i = new ImageView(mContext);
i.setImageResource(imgList.get(position).intValue());
i.setScaleType(ImageView.ScaleType.FIT_XY);
int size[] = new int[2];
size = (int[]) imgSize.get(position);
i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
return i;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></Gallery>
</LinearLayout>