ListView显示图片失真

41 篇文章 0 订阅

用listview显示快捷方式图片列表,图片一直失真,猜测各种问题。以为是获取Icon的方法有问题,但是把获取到的Icon显示在图片控件时又没问题,那就知道是ListView显示图片的问题了,网上找原因,看到别人的解释。

大家对于ImageList的问题归纳了一下,主要都是关于:  
  1.ImageList里面的图片的颜色  
  2.ImageList里面的图片的大小  
   
   
  /********* 1)ImageList里面的图片的颜色的问题 ********/  
   
   
  引起ImageList里面图片颜色失真的原因是在Design-Time就在VS.NET中往ImageList里面添加了Images。  
   
  当用户一边在“Image Collection Editor”对话框里面添加图片,VS.NET一边就已经把这些图片装载到resource文件里面了。这样,以后程序运行时就只需要访问resource文件就可以载入所有图片而不需要依赖原始的图片文件。  
   
  但是问题在于从结果看,当VS.NET在Design-Time往resource文件里面添加图片时并没有使用用户指定的ColorDepth(例如Depth32Bit),而用了ImageList.ColorDepth的默认值(Depth8Bit)。这样,等程序运行时,即使ImageList.ColorDepth指定了Depth32Bit也无济于事,因为原始的素材本身只有8bit的颜色。这基本上就是waki的问题的原因。  
   
  因此,解决方案是:不在Design-Time用VS.NET往ImageList里面添加图片,而是在程序运行时先指定32Bit的ColorDepth,然后再添加图片,如以下例子代码:  
   
  this.imageList1.ColorDepth=ColorDepth.Depth32Bit;  
  this.imageList1.Images.Add(Image.FromFile(@"C:\Inetpub\wwwroot\winxp.gif"));  
  this.imageList1.Images.Add(Image.FromFile(@"C:\Inetpub\wwwroot\images\init_dotnet.gif"));  
  this.imageList1.Images.Add(Image.FromFile(@"C:\Inetpub\wwwroot\images\mslogo.gif"));  
  this.imageList1.Images.Add(Image.FromFile(@"C:\Inetpub\wwwroot\images\mslogo2.gif"));  
   
  这里需要注意的是,必须先指定ColorDepth,然后再添加图片。因为对ColorDepth赋值会清空所有图片。BTW,ImageList.ColorDepth的默认值是Depth8Bit,而非文档上所述Depth4Bit。这一点很容易可以通过写一段例子代码来验证,也可以通过很多Decompiler来查看ImageList的构造函数的实现来验证。  
   
   
  /********** 2)ImageList里面的图片的大小 ************/  
   
   
  的确,通过ImageList.Images[i]获得图片的大小都是统一的,都等于ImageList.ImageSize。这个问题的原因在于ImageList在返回Images[i]的过程中并没有返回原始的图片,而是按照ImageList.ImageSize创建了一个新的Bitmap,并把原始图片的内容重新绘制上去以后再返回给用户。关于这一点,可以用一些Decompiler工具如ILDASM.exe或者Anakrino通过察看私有函数ImageList.GetBitmap(int index)来验证。我想现在paulluo0739应该能够理解为什么ImageList里面的图片都是一样大的了。  
   
  ImageSize同ColorDepth类似,也不宜在运行时改动,一旦重新赋值,就会清空所有的图片。因此,如果程序运行时需要某一图片的不同大小的版本,可以考虑使用多个不同ImageSize的ImageList。  
   
   
  希望以上这些能够对大家有帮助,感谢大家关心和使用微软的技术和产品。  
   
    ImageList il = new ImageList();
    il.ImageSize = new Size(32, 32);
    il.ColorDepth = ColorDepth.Depth32Bit;
    listView.SmallImageList = new ImageList();
    listView.LargeImageList = il;
    listView.View = View.Tile;
   
 解决办法帖子链接: http://topic.csdn.net/u/20080412/19/e3c757bd-bd77-4efc-bdc1-82be6779a753.html


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Android ListView显示图片,您可以使用自定义适配器来实现。以下是一个简单的示例: 1. 首先,在ListView布局文件中添加一个ImageView。 2. 在自定义适配器中,使用getView方法来设置每个列表项的布局和内容。在这个方法中,您可以使用setImageResource或setImageBitmap方法将图片设置为ImageView。 3. 在您的Activity中,将适配器设置为ListView的适配器。 以下是一个简单的示例代码: 首先,布局文件中添加ImageView: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` 然后,在适配器中设置布局和内容: ``` public class MyAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; private final int[] images; public MyAdapter(Context context, String[] values, int[] images) { super(context, R.layout.list_item, values); this.context = context; this.values = values; this.images = images; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_item, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); textView.setText(values[position]); imageView.setImageResource(images[position]); return rowView; } } ``` 最后,在Activity中将适配器设置为ListView的适配器: ``` ListView listView = (ListView) findViewById(R.id.listview); String[] values = new String[] { "Item 1", "Item 2", "Item 3" }; int[] images = new int[] { R.drawable.image1, R.drawable.image2, R.drawable.image3 }; MyAdapter adapter = new MyAdapter(this, values, images); listView.setAdapter(adapter); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值