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

很多时候,我们需要对一张图片进行显示,可能有写应用,显示的图片不是很大,一般几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();
		}

	}


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

/**
	 * 
	 * @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;
	}


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







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值