【Android】使用Gallery组件实现循环显示图像
刚刚写完博文http://blog.csdn.net/jueblog/article/details/12209969 之后,就想着能不能让图片循环播放呢?
试验出一个简便易行的方法。
Activity作如下改进:
只需修改第36行即可:
package com.app.adapter;import com.app.test01.R;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { /* 重写方法getCount,传回图片数目总数 */ return Integer.MAX_VALUE; } public Object getItem(int position) {/* 重写的getItem,传回position */ return position; } public long getItemId(int position) {/*重写方法getItemId,传回position */ return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); i.setImageResource(mImageIds[position%mImageIds.length]);/* 设定图片给imageView对象 */ i.setLayoutParams(new Gallery.LayoutParams(150,75)); return i; } public Integer[] getmImageIds() {return mImageIds;}public void setmImageIds(Integer[] mImageIds) {this.mImageIds = mImageIds;}private Integer[] mImageIds = { R.drawable.image01, R.drawable.image02, R.drawable.image03, R.drawable.image04, R.drawable.image05, R.drawable.image06, R.drawable.image07, R.drawable.image08, R.drawable.image09, };}