ImageResizer.java 文件源码

java
阅读 38 收藏 0 点赞 0 评论 0

项目:DisplayingBitmaps 作者:
/**
 * Decode and sample down a bitmap from resources to the requested width and height.
 *
 * @param res The resources object containing the image data
 * @param resId The resource id of the image data
 * @param reqWidth The requested width of the resulting bitmap
 * @param reqHeight The requested height of the resulting bitmap
 * @param cache The ImageCache used to find candidate bitmaps for use with inBitmap
 * @return A bitmap sampled down from the original with the same aspect ratio and dimensions
 *         that are equal to or greater than the requested width and height
 */
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight, ImageCache cache) {

    // BEGIN_INCLUDE (read_bitmap_dimensions)
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // END_INCLUDE (read_bitmap_dimensions)

    // If we're running on Honeycomb or newer, try to use inBitmap
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache);
    }

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号