public boolean inJustDecodeBounds;
public int inSampleSize;
public int inDensity;
public int inTargetDensity;
public int inScreenDensity;
public boolean inScaled;
public Bitmap.Config inPreferredConfig;
public int outWidth;
public int outHeight;
public String outMimeType;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeResource(getREsources(), R.drawable.XXX, options);
System.out.println(bitmap);
System.out.println(options.outWidth+" " +options.outHeight+" "+options.outMimeType);
结果:
null
XX, XX, XX
(1) 获取图片的原始的宽高,通过将Options对象的inJustDecodeBounds属性设置为true后调用decodeResource()函数,实现不真正加载图片(不耗费内存的情况下)获取图片的信息
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeREsource(getREsource(), resId, options);
(2) 根据据原始宽高和目标宽高计算出inSampleSize
public static int calSampleSize(BitmapFactory.Options options, int dstWidth, int dstHeight){
//原始宽高
int rawWidth = options.outWidth;
int rawHeight = options.outHeight;
if(rawWidth > dstWidth || rawHeight > dstHeight){
//得到图片比目标控件高大的倍数
float ratioHeight = (float) rawheight/dstHeight;
//得到图片比目标控件宽大的倍数
float ratioWidth = (float) rawWidth.dstWidth;
//两者中取最小的以便于覆盖控件不失真
inSampleSize = (int)Math.min(ratioWidth, ratioHeight);
}
return inSampleSize;
}
(3) 根据采样率解析出压缩后的Bitmap
BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inSampleSize = sampleSize;
try{
Bitmap bmp = BitmapFactory.decodeREsource(getResources(), R.drawable.XXX, options2);
...
}catch(OutOfMemoryError e){
...
}
文件夹 | drawable-ldpi | drawable-mdpi | drawable-hdpi | drawable-xhdpi | drawable-xxhdpi | drawable-xxxhdpi |
---|---|---|---|---|---|---|
density | 1 | 1.5 | 2 | 3 | 3.5 | 4 |
densityDpi | 160 | 240 | 320 | 480 | 560 | 640 |
dendity:表示dpi和px的换算比例,1dpi = 1density px
densityDpi:表示在对应的分辨率下每英寸有多少个dpi
屏幕上1英寸长 所对应的px数 = 每英寸的dpi数(densityDpi) * dpi所对应的px数(density)
如果屏幕的分辨率和这些文件所在文件夹的分辨率不同时:
Bitmap大小的计算:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.niuniuya);
System.out.println(bitmap.getWidth()+" "+bitmap.getHeight()+" "+bitmap.getByteCount());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.niuniuya, options);
System.out.println(bmp.getWidth()+" "+bmp.getHeight()+" "+bitmap.getByteCount());
结果
1200 1200 5760000
400 400 640000
options.inScaled = true;
options.inDensity = 1;
options.inTargetDensity = 2;
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.niuniuya, options);
System.out.println(bmp.getWidth()+" "+bmp.getHeight()+" "+bmp.getByteCount());
结果:
400 400 640000
800 800 2560000
options.inScaled = true;
options.inDensity = 1;
options.inTargetDensity = 2;
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.niuniuya, options);
System.out.println(bmp1.getWidth()+" "+bmp1.getHeight()+" "+bmp1.getByteCount());
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.niuniuya, options);
System.out.println(bmp2.getWidth()+" "+bmp2.getHeight()+" "+bmp2.getByteCount());
结果:
宽和高是不会变的
800 800 2560000
800 800 1280000