简体   繁体   English

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

[英]How to scale Bitmaps according to the screen Estate available

I just used this code form developers site , 我只是使用了此代码表单开发人员网站,

    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);
}

By Using this code I am really successfully calling the scaled bitmaps to my ImageViews using this line of code, 通过使用此代码,我确实可以使用此代码行成功地将缩放后的位图调用到我的ImageView中,

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

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

This line of code is really scaling my Images to any size I desired. 这行代码实际上将我的图像缩放到所需的任何大小。

But the problem I am facing here is ,"OUT OF MEMORY EXCEPTION." 但是我在这里面临的问题是“内存不足”。

  • The maximum size of Image I have is 1280*720 for now. 目前,我拥有的图像最大尺寸为1280 * 720。

- How to scale Bitmaps according to the screen Estate available. - 如何根据可用的屏幕遗产缩放位图。

I framed My code according to the densities of the device using: 我使用以下设备根据设备的密度来构造代码:

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

This will return Screen Densities such as 160,320,480,600,720,.... 这将返回屏幕密度,例如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);

The first time I run this on a device I realized that I was on a wrong track, for some of the Local Phablets, Tablets, mobile that have Large screen has less screen density. 第一次在设备上运行该设备时,我意识到我走错了路,对于某些具有大屏幕的本地平板电脑,平板电脑来说,其屏幕密度更低。

For example, 例如,

  • Micromax CanvasA116 has screen Resolution of more than 1280*720, but falls under a density of less than 320 integer value. Micromax CanvasA116的屏幕分辨率大于1280 * 720,但密度小于320整数值。
  • Some Local Tablet with same screen resolution has screen density of less than 160. 某些具有相同屏幕分辨率的本地平板电脑的屏幕密度小于160。
  • Sony Xperia E With Screen Resolution of 320*480 is coming under the Screen Density of 320. 屏幕分辨率为320 * 480的Sony Xperia E的屏幕密度为320。

So, on Xperia E kind of devices my above equation is working perfectly. 因此,在Xperia E类设备上,我的上述等式运行良好。 But for devices with High Screen Resolution, But on Low screen Density devices, the applied images are really BLURRED or OUTSHAPED completely beyond recognition. 但是对于具有高屏幕分辨率的设备,但在低屏幕密度的设备上,所应用的图像实际上是模糊的或不完整的,完全无法识别。

I also tried using the factor Screen width and Height. 我也尝试过使用屏幕宽度和高度因子。 But it is too fatal with devices having low heap. 但是对于低堆的设备来说,这太致命了。

So, The question is : 因此,问题是:

How can I perfectly Scale my Bitmaps accordingly to the devices irrespective of density & resolutions. 无论密度和分辨率如何,我如何都能根据设备完美缩放我的位图。 As in this condition the width & height of the Bitmpa are completely unknown! 在这种情况下,Bitmpa的宽度和高度是完全未知的!

I hope this code is helpful for you, check 我希望这段代码对您有帮助,请检查

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM