Android 如何在网格视图中动态显示多个图像 私有上下文mContext; private int[]colors=new int[]{Color.WHITE,0x30aaaaaa}; 私有int[]点颜色=新int[7]; 私人名单; 私有任务数据源; 私有int con=1000; 公共整数[]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 }; 公共ImageAdapter(上下文、列表内容){ sta=内容; 数据源=新任务数据源(上下文);//此处 datasource.open(); mContext=上下文; } @凌驾 public int getCount(){ 返回mThumbIds.length; } @凌驾 公共对象getItem(int位置){ 返回mThumbIds[位置]; } @凌驾 公共长getItemId(int位置){ 返回0; } @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ int[]st=新的int[sta.size()]; ImageView ImageView=新的ImageView(mContext); setImageResource(mThumbIds[position]); imageView.setScaleType(imageView.ScaleType.CENTER\U裁剪); setLayoutParams(新的GridView.LayoutParams(20,20)); 返回图像视图; }
我有一个动态初始化的循环,在前面的代码中有一些改动Android 如何在网格视图中动态显示多个图像 私有上下文mContext; private int[]colors=new int[]{Color.WHITE,0x30aaaaaa}; 私有int[]点颜色=新int[7]; 私人名单; 私有任务数据源; 私有int con=1000; 公共整数[]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 }; 公共ImageAdapter(上下文、列表内容){ sta=内容; 数据源=新任务数据源(上下文);//此处 datasource.open(); mContext=上下文; } @凌驾 public int getCount(){ 返回mThumbIds.length; } @凌驾 公共对象getItem(int位置){ 返回mThumbIds[位置]; } @凌驾 公共长getItemId(int位置){ 返回0; } @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ int[]st=新的int[sta.size()]; ImageView ImageView=新的ImageView(mContext); setImageResource(mThumbIds[position]); imageView.setScaleType(imageView.ScaleType.CENTER\U裁剪); setLayoutParams(新的GridView.LayoutParams(20,20)); 返回图像视图; },android,android-layout,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
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=新整数[con];
对于(int i=0;i 而(ii使用通用图像加载器。
它基于惰性列表(工作原理相同)。可以在本地加载图像或形成服务器。
但它有很多其他配置。我更喜欢使用通用映像加载程序,因为它提供了更多配置选项。如果downlaod失败,您可以显示错误映像。可以显示圆角映像。可以在光盘或内存中缓存。可以压缩映像
在自定义适配器构造函数中
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;
}
}
在getView()中
根据位置显示图像
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();
您可以配置其他选项以满足您的需要
使用通用图像加载器,您可以查看保持架以实现平滑滚动和性能。如何使用displayImage(mThumbIds[position])当mThumbIds是一个整数并且它要求一个字符串时,我在尝试时遇到了一个错误。@RED_u从url加载示例,虽然这是url,但我在int中使用了大量本地可绘制内容。它告诉我应该有一个字符串。我将回头看tutorials@RED_使用适合您需要的方法。检查可用的方法霍兹
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(mThumbIds[position], image,options);//provide imageurl, imageview and options.