当获得固定的高度和宽度照片时,“createBitmap”和“createScaledBitmap”有什么区别

时间:2015-10-31 11:28:53

标签: android bitmap

我想要导入一张照片,然后在ImageView中显示它的“高度”和“宽度”是固定的。首先,我输入BitmapFactoryoptions.inSampleSize的点动以获取位图。其次,我想要获取照片,但我不知道我可以在createScaledBitmap(Bitmap src, int dstWidth, int dstHeight)createBitmap(Bitmap source, int x, int y, int width, int height)中使用哪些照片。

我看到了Android Developer个文档,但我仍然模糊。我认为createScaledBitmap已经改变了照片的大小,但createBitmap可以随意剪切照片。< / p>

 public static Bitmap getBitmap(String filePath, String fileName, int reqWidth, int reqHeight) {


    if (filePath.length() <= 0 || filePath == null) {
        return null;
    }
    String fullPath = filePath+"/"+fileName;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(fullPath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    Bitmap photoBitmap = BitmapFactory.decodeFile(fullPath, options);

    return photoBitmap;
}


public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

0 个答案:

没有答案