private Context mContext;
private int[] colors = new int[] { Color.WHITE, 0x30aaaaaa };
private int[] dotColors = new int[7];
private List<Integer> sta;
private TasksDataSource datasource;
private int con=1000;
public Integer[] mThumbIds = {
R.drawable.green, R.drawable.green,
R.drawable.green, R.drawable.green,
R.drawable.green, R.drawable.green,
R.drawable.green, R.drawable.green,
R.drawable.green, R.drawable.green,
R.drawable.green, R.drawable.green,
R.drawable.green, R.drawable.green,
R.drawable.green
};
public ImageAdapter(Context context, List<Integer> content){
sta = content;
datasource = new TasksDataSource(context); //here
datasource.open();
mContext=context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int[] st = new int[sta.size()];
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(20, 20));
return imageView;
}
我有一个用于动态初始化的循环,它在前面的代码中有一些改变…
public Integer[] mThumbIds = new Integer[con];
for(int i = 0;i < st.length;i++)
{
st[i] = sta.get(i);
Log.i("st::" + st[i]," ");
}
int ii=0,jj=0;
while(ii<con)
{
if(st[jj]==0)
{
mThumbIds[ii]=R.drawable.red;
}
else if(st[jj]==1)
{
mThumbIds[ii]=R.drawable.green;
}
else
{
mThumbIds[ii]=R.drawable.grey;
}
}
上面的加法不起作用,无限运行并停止。我想要的是,当mThumbIds[ii]为0时显示红色图像,当mThumbIds[ii]为1时显示绿色图像,当mThumbIds[ii]为2时显示灰色图像。我怎样才能做到这一点??
使用通用图像加载器
https://github.com/nostra13/Android-Universal-Image-Loader。
它基于Lazy List(工作原理相同)。可以在本地加载图像或形成服务器。但是它有很多其他的构型。我更喜欢使用Universal Image Loader,因为它给了你更多的配置选项。如果下载失败,可以显示错误图像。可以显示圆角图像。可以在磁盘或内存上缓存。可以压缩图像。
在自定义适配器构造函数
中 public Integer[] mThumbIds = {
R.drawable.red, R.drawable.green,
R.drawable.grey, .....
};
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
在getView()
根据位置显示图像。
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(mThumbIds[position], image,options);//provide imageurl, imageview and options.
你可以根据需要配置其他选项。
与通用图像加载器一起,您可以查看持有人平滑滚动和性能。http://developer.android.com/training/improving-layouts/smooth-scrolling.html。