前段时间公司开发的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;
- }
转载一个大牛:http://blog.csdn.net/chenshijun0101/article/details/38295225