package com.zncm.androidutils.ui;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Gallery;
import com.zncm.androidutils.ui.adapter.GalleryAdapter2;
public class GalleryDemo extends Activity {
int[] images = {R.drawable.g1, R.drawable.g2, R.drawable.g3, R.drawable.g4, R.drawable.g5,
R.drawable.g6, R.drawable.g7, R.drawable.g8};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_demo);
GalleryAdapter2 adapter2 = new GalleryAdapter2(this, images);
Gallery gallery = (Gallery) findViewById(R.id.gallery_demo_gallery1);
gallery.setSpacing(20);// 图片间隔
gallery.setAdapter(adapter2);
}
}
ImageAdapter
package com.zncm.androidutils.ui.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class GalleryAdapter2 extends BaseAdapter {
private Context mContext;
private int mImageIds[];
public GalleryAdapter2(Context ctx, int imageIds[]) {
this.mContext = ctx;
this.mImageIds = imageIds;
}
@Override
public int getCount() {
return mImageIds.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 imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
return imageView;
}
}
转载于:https://my.oschina.net/dminter/blog/205047