从SDCard获取的图片按分辨率处理的方法

前段时间公司开发的Launcher要做主题切换的功能,但切换主题时须要从sdcard中获取要切换的图片资源,拿到后图片的大小不正常。

后来查找原因是:系统对不同分辨率拿到的图片资源会自己主动的做转化,所以如今要做的是把图片按不同的分辨率转化成图片实际的大小

代码转化例如以下:

 

从SD卡获取的图片按分辨率处理
public static Bitmap scaleImage(Bitmap bmp,int displayWidth,int displayHeight) {
  		float scaleWidth = 1,scaleHeight = 1;
        int bmpWidth = bmp.getWidth();
        int bmpHeight = bmp.getHeight();
		if (displayWidth >= 480 && displayWidth <720) {
	        double scale = 0.75;
	        scaleWidth = (float) (scaleWidth * scale);
	        scaleHeight = (float) (scaleHeight * scale);
	        Matrix matrix = new Matrix();
	        matrix.postScale(scaleWidth, scaleHeight);
	          return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		} else if (displayWidth >= 720 && displayWidth < 1080) {
		     return bmp;
		} else if (displayWidth >= 1080) {
			 double scale = 1.5;
		     scaleWidth = (float) (scaleWidth * scale);
		     scaleHeight = (float) (scaleHeight * scale);
		     Matrix matrix = new Matrix();
		     matrix.postScale(scaleWidth, scaleHeight);
		     return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		}
		return bmp;
  	}

posted @ 2017-07-24 18:50  mfmdaoyou  阅读(200)  评论(0编辑  收藏  举报