Android - 确认ImageView的图片是否加载成功

来源:互联网 发布:网络厂商排名 编辑:程序博客网 时间:2024/06/16 16:39

本文地址:http://blog.csdn.net/caroline_wendy


判断ImageView图片是否存在,未加载成功,就显示默认图片。 
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. if (mPhotoView.getDrawable() == null) {  
  2.         Toast.makeText(getActivity(), "图片加载失败", Toast.LENGTH_SHORT).show();  
  3.       mPhotoView.setImageDrawable(getResources().getDrawable(R.drawable.icon_default_user_photo));  
  4.   }  


Android中Bitmap, Drawable, Byte,ID之间的转化

1.  Bitmap 转化为 byte
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ByteArrayOutputStream out = new ByteArrayOutputStream();  
  2. bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  
  3. byte[] array= out.toByteArray();  

2. byte转化为bitmap
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  

3. bitmap转化为Drawable
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Drawable drawable = new FastBitmapDrawable(bitmap);  

4. Drawable转化为bitmap
a. BitmapDrawable, FastBitmapDrawable直接用getBitmap
b. 其他类型的Drawable用Canvas画到一个bitmap上
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Canvas canvas = new Canvas(bitmap)  
  2. drawable.draw(canvas)  

5.id转化graphic.drawable
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Drawable drawable = activity.getResources().getDrawable(R.drawable.icon);  

6.id转化成Bitmap
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Bitmap bitmap = BitmapFactory. decodeResource (Resources   res, int id)  
0 0
原创粉丝点击