android中关于图片的处理和显示

很多时候,我们需要对一张图片进行显示,可能有写应用,显示的图片不是很大,一般几K到几百K而已,但是若几M到几十M或者很多张几百K的图片呢,这个时候去直接显示,很可能会出现内存溢出的可能,在android中,虚拟机默认给我们分配的内存大小为16M(具体数字不记得了,可以看官方文档),那么当你显示很多张图片时,并且每张图片的大小都是几M的时候,可能你的程序运行不了多久,就会出现溢出了。所以我们需要对图片进行处理,也就是将它缩小,让它原来的几M变成几K,当然,我说的是图片预览的时候对它进行缩小,真正显示的时候所看到的应该还是它的实际大小,不然,图片就失真了。看看下面的处理吧:

方法一:直接对图片进行矩阵的变化,这种方法我不知道图片的具体大小变化了没,你可以自己试试:

[java]  view plain copy print ?
  1. /** 
  2.      *  
  3.      * @param bitmap 原图,原图要是一个Bitmap对象,否则转不了 
  4.      * @param width 目标宽度 
  5.      * @param height 目标高度 
  6.      * @return 
  7.      */  
  8.     protected static Bitmap matrixBitmap(Bitmap bitmap, int width, int height) {  
  9.         if (bitmap == null) {  
  10.             return null;  
  11.         }  
  12.         int w = bitmap.getWidth();  
  13.         int h = bitmap.getHeight();  
  14.         Matrix matrix = new Matrix();  
  15.         float scaleWidth = ((float) width / w);//开始计算,按照原图和目标的尺寸比例,得到宽和高分别缩放的比例大小  
  16.         float scaleHeight = ((float) height / h);  
  17.         matrix.postScale(scaleWidth, scaleHeight);  
  18.         Bitmap newbmp = Bitmap.createBitmap(bitmap, 00, w, h, matrix, true);//按照一定的比例大小,得到新的Bitmap对象  
  19.         return newbmp;  
  20.     }  

方法二:对文件进行缩放,在显示缩略图时,显示小图,点击的时候,显示实际图片的大小:

[java]  view plain copy print ?
  1. /** 
  2.      * @param path 
  3.      *            图片文件的路径 
  4.      * @param width 
  5.      *            目标宽度 
  6.      * @param height 
  7.      *            目标高度 
  8.      * @return 
  9.      */  
  10.     public static Bitmap zoomBitmap(String path, int width, int height) {  
  11.           
  12.         BitmapFactory.Options options = new BitmapFactory.Options();  
  13.           
  14.         /* 
  15.          * 官方文档这样写着“If set to true, the decoder will return null (no bitmap), 
  16.          * but the out... fields will still be set, allowing the caller to query 
  17.          * the bitmap without having to allocate the memory for its 
  18.          * pixels. ”,大意就是说inJustDecodeBounds 
  19.          * 为true时,不给出实际的Bitmap对象,但你可以得到这张图片的实际信息,这样你就可以计算缩放 比例了 
  20.          */  
  21.         options.inJustDecodeBounds = true// 下面的bitmap暂时为null  
  22.           
  23.         Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bitmap为空,不占用内存的  
  24.         int multiple = (int) (options.outHeight / 54); // 计算缩放值  
  25.         if (multiple <= 0// 如果缩放值小于0,则不对图片进行缩放  
  26.             multiple = 1;  
  27.           
  28.         options.inSampleSize = multiple;  
  29.         options.inJustDecodeBounds = false// 得到缩放后的Bitmap对象  
  30.         bitmap = BitmapFactory.decodeFile(path, options); //  
  31.         return bitmap;  
  32.     }  

以上两种方法,也就得到了Bitmap对象了。

那么我们来将Bitmap对象转换成我们所需要的图片格式文件了:

[java]  view plain copy print ?
  1. /** 
  2.  *  
  3.  * @param bitmap 
  4.  *            远Bitmap对象 
  5.  * @param picPath 
  6.  *            转换成文件时的路径地址 
  7.  */  
  8. public static void bitmapToPic(Bitmap bitmap, String picPath) {  
  9.     File file = new File(picPath);  
  10.     try {  
  11.         FileOutputStream out = new FileOutputStream(file);  
  12.         if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {  
  13.             out.flush();  
  14.             out.close();  
  15.         }  
  16.     } catch (Exception e) {  
  17.         e.printStackTrace();  
  18.     }  
  19.   
  20. }  


转换成文件之后,到此我们也就快结束了。突然想起来,有些时候,我们需要对背景图片进行圆角处理,处理的方式有很多中,不过无外乎xml文件配置,这里我们给出一种代码处理的方式,看下面的代码:

[java]  view plain copy print ?
  1. /** 
  2.      *  
  3.      * @param bitmap 
  4.      *            源Bitmap独享 
  5.      * @param pixels 
  6.      *            圆角的像素值 
  7.      * @return 
  8.      */  
  9.     protected static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  
  10.         if (bitmap == null) {  
  11.             return null;  
  12.         }  
  13.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
  14.                 bitmap.getHeight(), Config.ARGB_8888);  
  15.         Canvas canvas = new Canvas(output);  
  16.         int color = 0xff424242// 透明值,前面的0xff是透明度,后面的424242是rgb色值  
  17.         Paint paint = new Paint();  
  18.         Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
  19.         RectF rectF = new RectF(rect);  
  20.         if (pixels < 1) {  
  21.             pixels = IUtils.DEFPIXELS;  
  22.         }  
  23.         float roundPx = pixels;  
  24.         paint.setAntiAlias(true);  
  25.         canvas.drawARGB(0000);  
  26.         paint.setColor(color);  
  27.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  28.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  29.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  30.         return output;  
  31.     }  


到此,我们的图片处理,也就完成了。上面的这些问题,都是我在项目开发中所遇到过的问题,刚开始做的时候很生疏,但到后来,也就慢慢习惯了,这个看似不难的背后,却也付出了我不少的心血呀!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值