如何在网格视图中动态显示多个图像

时间:2013-04-03 08:07:28

标签: android android-layout

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;
          }
      }

以上的补充不能正常运行并无限运行并被停止。 我想要的是,显示红色img wen mThumbIds [ii]为0,当mThumbIds [ii]为1时为绿色,当mThumbIds [ii]为2时为灰色。我怎么能实现这个?

1 个答案:

答案 0 :(得分:0)

使用通用图片加载器。

https://github.com/nostra13/Android-Universal-Image-Loader

它基于Lazy List(基于相同的原理)。可以在本地加载图像或构成服务器。 但它有很多其他配置。我更喜欢使用通用图像加载器因为它为您提供了更多配置选项。如果下载失败,您可以显示错误图像。可以显示带圆角的图像。可以缓存在光盘或内存上。可压缩图像。

在自定义适配器构造函数

 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.

您可以配置其他选项以满足您的需求。

与Universal Image Loader一起,您可以查看支架以实现平滑滚动和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html

相关问题