main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<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_height="fill_parent"
android:layout_width="fill_parent"
android:spacing="20px"/>
</LinearLayout>
写一个类继承BaseAdapter,来承载图片资源。最后Gallery来加载。
public class QhAlbumActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("QhAlbum", "+ onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
class ImageAdapter extends BaseAdapter{
private int[] resArray = {R.drawable.q2,
R.drawable.q3,
R.drawable.q4,
R.drawable.q6,
R.drawable.q8,
R.drawable.q11,
R.drawable.q14,
R.drawable.q15,
R.drawable.q18,
R.drawable.q20};
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return resArray.length;
}
@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
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(resArray[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
Gallery gallery = (Gallery)findViewById(R.id.gallery);
ImageAdapter imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
}
}