很多时候,我们需要对一张图片进行显示,可能有写应用,显示的图片不是很大,一般几K到几百K而已,但是若几M到几十M或者很多张几百K的图片呢,这个时候去直接显示,很可能会出现内存溢出的可能,在android中,虚拟机默认给我们分配的内存大小为16M(具体数字不记得了,可以看官方文档),那么当你显示很多张图片时,并且每张图片的大小都是几M的时候,可能你的程序运行不了多久,就会出现溢出了。所以我们需要对图片进行处理,也就是将它缩小,让它原来的几M变成几K,当然,我说的是图片预览的时候对它进行缩小,真正显示的时候所看到的应该还是它的实际大小,不然,图片就失真了。看看下面的处理吧:
方法一:直接对图片进行矩阵的变化,这种方法我不知道图片的具体大小变化了没,你可以自己试试:
- /**
- *
- * @param bitmap 原图,原图要是一个Bitmap对象,否则转不了
- * @param width 目标宽度
- * @param height 目标高度
- * @return
- */
- protected static Bitmap matrixBitmap(Bitmap bitmap, int width, int height) {
- if (bitmap == null) {
- return null;
- }
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- Matrix matrix = new Matrix();
- float scaleWidth = ((float) width / w);//开始计算,按照原图和目标的尺寸比例,得到宽和高分别缩放的比例大小
- float scaleHeight = ((float) height / h);
- matrix.postScale(scaleWidth, scaleHeight);
- Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);//按照一定的比例大小,得到新的Bitmap对象
- return newbmp;
- }
方法二:对文件进行缩放,在显示缩略图时,显示小图,点击的时候,显示实际图片的大小:
- /**
- * @param path
- * 图片文件的路径
- * @param width
- * 目标宽度
- * @param height
- * 目标高度
- * @return
- */
- public static Bitmap zoomBitmap(String path, int width, int height) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- /*
- * 官方文档这样写着“If set to true, the decoder will return null (no bitmap),
- * but the out... fields will still be set, allowing the caller to query
- * the bitmap without having to allocate the memory for its
- * pixels. ”,大意就是说inJustDecodeBounds
- * 为true时,不给出实际的Bitmap对象,但你可以得到这张图片的实际信息,这样你就可以计算缩放 比例了
- */
- options.inJustDecodeBounds = true; // 下面的bitmap暂时为null
- Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bitmap为空,不占用内存的
- int multiple = (int) (options.outHeight / 54); // 计算缩放值
- if (multiple <= 0) // 如果缩放值小于0,则不对图片进行缩放
- multiple = 1;
- options.inSampleSize = multiple;
- options.inJustDecodeBounds = false; // 得到缩放后的Bitmap对象
- bitmap = BitmapFactory.decodeFile(path, options); //
- return bitmap;
- }
以上两种方法,也就得到了Bitmap对象了。
那么我们来将Bitmap对象转换成我们所需要的图片格式文件了:
- /**
- *
- * @param bitmap
- * 远Bitmap对象
- * @param picPath
- * 转换成文件时的路径地址
- */
- public static void bitmapToPic(Bitmap bitmap, String picPath) {
- File file = new File(picPath);
- try {
- FileOutputStream out = new FileOutputStream(file);
- if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
- out.flush();
- out.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- *
- * @param bitmap
- * 源Bitmap独享
- * @param pixels
- * 圆角的像素值
- * @return
- */
- protected static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
- if (bitmap == null) {
- return null;
- }
- Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
- bitmap.getHeight(), Config.ARGB_8888);
- Canvas canvas = new Canvas(output);
- int color = 0xff424242; // 透明值,前面的0xff是透明度,后面的424242是rgb色值
- Paint paint = new Paint();
- Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
- RectF rectF = new RectF(rect);
- if (pixels < 1) {
- pixels = IUtils.DEFPIXELS;
- }
- float roundPx = pixels;
- paint.setAntiAlias(true);
- canvas.drawARGB(0, 0, 0, 0);
- paint.setColor(color);
- canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
- paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
- canvas.drawBitmap(bitmap, rect, rect, paint);
- return output;
- }
到此,我们的图片处理,也就完成了。上面的这些问题,都是我在项目开发中所遇到过的问题,刚开始做的时候很生疏,但到后来,也就慢慢习惯了,这个看似不难的背后,却也付出了我不少的心血呀!!!