Bitmap能够经过一个Bitmap建立新的Bitmap。程序员
总之:Bitmap为咱们程序员提供了对图像文件的操做支持。(就像File类为咱们提供了对本地本件的操做支持同样)web
步骤:数组
注意: svg
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calulateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
private static int calulateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
if (reqWidth == 0 || reqHeight == 0) {
return 1;
}
int outHeight = options.outHeight;
int outWidth = options.outWidth;
int inSampleSize = 1;
if (outHeight > reqHeight || outWidth > reqWidth) {
int halfHeight = outHeight / 2;
int halfWidth = outWidth / 2;
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.testbitmap, 300, 300);
imageView.setImageBitmap(bitmap);
说明:建立drawable-xxxhdpi文件夹,文件夹下放一张3840*2160的图片,而后咱们用bitmap来加载为300x300的图片。如文章开头图片。性能