繁体   English   中英

如何在Android中以高质量缩放位图而不会崩溃

[英]How to scale bitmap with high quality and without crash in android

我正在使用url下载图像,但是缩放后会崩溃并且缩放不正确。 这是我使用的代码

//Code to fetch bitmap  
Bitmap bmp=HttpFetch.fetchBitmap(imageUrl);

//Scale bitmap

Bitmap myBitmap = getResizedBitmap(bmp, viewWidth, viewHeight);  

public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        int srcWidth = bm.getWidth();
        int srcHeight = bm.getHeight();

        int desiredWidth = newWidth;
        int desiredHeight = newHeight;

        int inSampleSize = 1;
        while(srcWidth / 2 > desiredWidth){
            srcWidth /= 2;
            srcHeight /= 2;
            inSampleSize *= 2;
        }

        float desiredWidthScale = (float) desiredWidth / srcWidth;
        float desiredHeightScale = (float) desiredHeight / srcHeight;

        // Decode with inSampleSize
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inSampleSize = inSampleSize;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Matrix matrix = new Matrix();
        matrix.postScale(desiredWidthScale, desiredHeightScale);
        original = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return original;
}

无法找到使用位图获取代码BitmapFactory.Option的代码,以及大多数使用文件名的地方,是否可以使用位图获取BitmapFactory.Options?

从网络下载时,请提出更好的解决方案来缩放位图。

original = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

在调用getResizedBitmap()之后,此行返回一个不变的位图,如果您尝试修改此位图,则可能会导致崩溃。

安卓#createBitmap()

请查阅LogCat了解详细信息。

暂无
暂无

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

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