Android 计算ImageView的大小

来源:互联网 发布:博德之门2增强版 mac 编辑:程序博客网 时间:2024/06/05 13:34

1.获取资源文件中图片的大小,最简单的最直接的方法,就是使用Drawable的getIntrinsicHeight()和getIntrinsicWidth();

2.利用Bitmap来获取其大小,本质上和第一种方式没什么区别:

 

Java代码  收藏代码
  1. /** 
  2.  * 计算ImageView的大小(BitmapDrawable) 
  3.  *  
  4.  * @param resources 
  5.  * @param resourceId 
  6.  * @return 
  7.  */  
  8. public static int[] computeWH(Resources resources, int resourceId) {  
  9.     int[] wh = { 00 };  
  10.     if (resources == null)  
  11.         return wh;  
  12.     Bitmap mBitmap = BitmapFactory.decodeResource(resources, resourceId);  
  13.     BitmapDrawable bDrawable = new BitmapDrawable(resources, mBitmap);  
  14.     wh[0] = bDrawable.getIntrinsicWidth();  
  15.     wh[1] = bDrawable.getIntrinsicHeight();  
  16.   
  17.     return wh;  
  18. }  

 

 3.可以利用BitmapFactory.Options的outWidth和outHeight两个参数获取其大小,下面给出简单的3种方法供参考:

注意:设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度。

Java代码  收藏代码
  1. /** 
  2.  * 计算ImageView的大小(decodeFileDescriptor) 
  3.  *  
  4.  * @param imageFile 
  5.  * @return 
  6.  */  
  7. public static int[] computeWH_1(String imageFile) {  
  8.     int[] wh = { 00 };  
  9.     if (imageFile == null || imageFile.length() == 0)  
  10.         return wh;  
  11.     try {  
  12.         FileDescriptor fd = new FileInputStream(imageFile).getFD();  
  13.         BitmapFactory.Options options = new BitmapFactory.Options();  
  14.         options.inSampleSize = 1;  
  15.         options.inJustDecodeBounds = true;  
  16.         BitmapFactory.decodeFileDescriptor(fd, null, options);  
  17.         if (options.mCancel || options.outWidth == -1  
  18.                 || options.outHeight == -1) {  
  19.             return wh;  
  20.         }  
  21.         wh[0] = options.outWidth;  
  22.         wh[1] = options.outHeight;  
  23.   
  24.     } catch (Exception e) {  
  25.     }  
  26.       
  27.     return wh;  
  28. }  

 

Java代码  收藏代码
  1. /** 
  2.  * 计算ImageView的大小(decodeFile) 
  3.  *  
  4.  * @param imgFile 
  5.  * @return 
  6.  */  
  7. public static int[] computeWH_2(String imgFile) {  
  8.     int[] wh = { 00 };  
  9.   
  10.     if (imgFile == null || imgFile.length() == 0)  
  11.         return wh;  
  12.   
  13.     BitmapFactory.Options options = new BitmapFactory.Options();  
  14.     options.inSampleSize = 1;  
  15.     options.inJustDecodeBounds = true;  
  16.     BitmapFactory.decodeFile(imgFile, options);  
  17.     if (options.mCancel || options.outWidth == -1  
  18.             || options.outHeight == -1) {  
  19.         return wh;  
  20.     }  
  21.   
  22.     wh[0] = options.outWidth;  
  23.     wh[1] = options.outHeight;  
  24.   
  25.     return wh;  
  26. }  
Java代码  收藏代码
  1. /** 
  2.  * 计算ImageView的大小(decodeResource) 
  3.  *  
  4.  * @param resources 
  5.  * @param resourceId 
  6.  * @return 
  7.  */  
  8. public static int[] computeWH_3(Resources resources, int resourceId) {  
  9.     int[] wh = { 00 };  
  10.     if (resources == null)  
  11.         return wh;  
  12.   
  13.     BitmapFactory.Options options = new BitmapFactory.Options();  
  14.     options.inSampleSize = 1;  
  15.     options.inJustDecodeBounds = true;  
  16.     BitmapFactory.decodeResource(resources, resourceId, options);  
  17.     if (options.mCancel || options.outWidth == -1  
  18.             || options.outHeight == -1) {  
  19.         return wh;  
  20.     }  
  21.   
  22.     wh[0] = options.outWidth;  
  23.     wh[1] = options.outHeight;  
  24.   
  25.     return wh;  
  26. }  
 转载地址:http://chjmars.iteye.com/blog/1156832
0 0
原创粉丝点击