首页 文章

如何根据屏幕可用的比例缩放位图

提问于
浏览
0

我刚用这个代码表开发者网站,

public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
    //Raw height & width of the image

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height>reqHeight || width>reqWidth) {
        //Calculate ratios of height & width to requested height & width.
        final int heightRatio = Math.round((float)height / (float)reqHeight);
        final int widthRatio = Math.round((float)width / (float)reqWidth);


        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    }
    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromresource(Resources res, int resId,int reqWidth,int reqHeight){
    //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);

    //Decode Bitmap with inSampleSize Set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeResource(res, resId, options);
}

通过使用此代码,我真的使用这行代码成功调用缩放的位图到我的ImageViews,

image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), resId, width, height));

image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 640, 360));

这行代码实际上是将我的图像缩放到我想要的任何大小 .

但我在这里遇到的问题是“内存不足” .

  • The maximum size of Image I have is 1280*720 for now.

  • How to scale Bitmaps according to the screen Estate available.

我根据设备的密度使用以下框架构建了我的代码:

int screenDensity = getResources().getDisplayMetrics().densityDpi;

这将返回屏幕密度,如160,320,480,600,720,....

ImageView image = new ImageView(this);
    if (screenDensity<=240) {
        image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 320, 180));
    } else if (screenDensity<=320) {
        image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 430, 240));
    } else if (screenDensity<=640) {
        image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 640, 360));
    } else {
        image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 1280, 720));
    } 
    background_image.addView(image);

我第一次在设备上运行它时,我意识到我在错误的轨道上,对于一些Local Phablets,平板电脑,具有大屏幕的移动设备具有较少的屏幕密度 .

例如,

  • Micromax CanvasA116的屏幕分辨率超过1280 * 720,但密度小于320整数值 .

  • 具有相同屏幕分辨率的某些本地平板电脑的屏幕密度小于160 .

  • 索尼Xperia E屏幕分辨率为320 * 480,屏幕密度为320 .

因此,在Xperia E类设备上,我的上述方程式工作正常 . 但对于具有高屏幕分辨率的设备,但在低屏幕密度设备上,所应用的图像实际上是无法识别的BLURRED或OUTSHAPED .

我也尝试使用因子Screen width和Height . 但是对于具有低堆的设备来说它太致命了 .

所以,问题是:

How can I perfectly Scale my Bitmaps accordingly to the devices irrespective of density & resolutions. 在这种情况下,Bitmpa的宽度和高度完全不为人知!

1 回答

  • 0

    我希望这段代码对你有所帮助,请检查

    public static Bitmap resizeImageWithOrignal(Bitmap orignal, int maxDimension) {
        // load the origial BitMap
        int width = orignal.getWidth();
        int height = orignal.getHeight();
    
        if (width > maxDimension || height > maxDimension) {
            int new_width;
            int new_height;
            float ratio = (float) width / height;
            if (ratio > 1.0f) {
                new_width = maxDimension;
                new_height = (int) ((float) new_width / ratio);
            } else {
                new_height = maxDimension;
                new_width = (int) ((float) new_height * ratio);
            }
            float scaleWidth = ((float) new_width) / width;
            float scaleHeight = ((float) new_height) / height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(orignal, 0, 0, width,
                    height, matrix, true);
            return resizedBitmap;
        }
        return orignal;
    }
    

相关问题