WEB开发网
开发学院手机开发Android 开发 本可循环显示图像的Android Gallery组件 阅读

本可循环显示图像的Android Gallery组件

 2010-11-05 00:56:16 来源:WEB开发网   
核心提示:private ImageAdapter imageAdapter;private int[] resIds = new int[]{ R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,R.drawable.item5, R.d

private ImageAdapter imageAdapter;

private int[] resIds = new int[]

{ R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,

R.drawable.item5, R.drawable.item6, R.drawable.item7,

R.drawable.item8, R.drawable.item9, R.drawable.item10,

R.drawable.item11, R.drawable.item12, R.drawable.item13,

R.drawable.item14, R.drawable.item15 };

public class ImageAdapter extends BaseAdapter

{

int mGalleryItemBackground;

private Context mContext;

public ImageAdapter(Context context)

{

mContext = context;

TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);

mGalleryItemBackground = typedArray.getResourceId(

R.styleable.Gallery_android_galleryItemBackground, 0);

}

// 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE

public int getCount()

{

return Integer.MAX_VALUE;

}

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 = new ImageView(mContext);

// 第2点改进,通过取余来循环取得resIds数组中的图像资源ID

imageView.setImageResource(resIds[position % resIds.length]);

imageView.setScaleType(ImageView.ScaleType.FIT_XY);

imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));

imageView.setBackgroundResource(mGalleryItemBackground);

return imageView;

}

}

@Override

public void onItemSelected(AdapterView parent, View view, int position,long id)

{

// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像

上一页  1 2 3 4 5 6 7  下一页

Tags:循环 显示 图像

编辑录入:coldstar [复制链接] [打 印]
赞助商链接