简体   繁体   中英

Android - Glide resizing image via "override" and "fitCenter" deforms Bitmap

I'm using Glide to load images from gallery into GLSurfaceView . However, when I tried to resize the image using override(width, height) , it does not do so. Thus, I added fitCenter() , which seems to be the key to get the desired size.

My problem is when the Bitmap is resized the result is quite weird. Everything is good except for images that have small width values. The attached pictures illustrate the difference between using and not using fitCenter() .

Here's the code I used for loading via Glide

Glide.with(this)
    .load(imageUri)
    .asBitmap()
    .override(newWidth, newHeight)
    .fitCenter()
    .atMost()
    .into(new SimpleTarget<Bitmap>(newWidth, newHeight) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            Log.e(TAG, "Loaded Bitmap Size :" + resource.getWidth() + "x" + resource.getHeight());
            /*
            .
            . Initialize GLSurfaceView with Bitmap resource
            .
            */
        }
    }) ;

I thought for a second that it may be a GLSurfaceView problem, something related to very small width I guessed. But it looks that it renders perfectly until the image is resized.

Is there anything wrong with my code? I would really appreciate any suggestion.

在此处输入图像描述

It looks like the problem occurs when using fitCenter() with GLSurfaceView with a Bitmap that has either width or height as an odd number. I solved the issue by adding the following custom transformation to Glide call.

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int maxWidth, int maxHeight) {
    if (maxHeight > 0 && maxWidth > 0) {
        int width = toTransform.getWidth();
        int height = toTransform.getHeight();

        float ratioBitmap = (float) width / (float) height;
        float ratioMax = (float) maxWidth / (float) maxHeight;

        int finalWidth = maxWidth;
        int finalHeight = maxHeight;
        if (ratioMax > ratioBitmap) {
            finalWidth = (int) ((float) maxHeight * ratioBitmap);
        } else {
            finalHeight = (int) ((float)maxWidth / ratioBitmap);
        }

        return Bitmap.createScaledBitmap(toTransform, previousEvenNumber(finalWidth), previousEvenNumber(finalHeight), true);
    }
    else {
        return toTransform;
    }
}

private int previousEvenNumber(int x){
    if ( (x & 1) == 0 )
        return x;
    else
        return x - 1;
}

And editing Glide call to the following:

Glide.with(this)
    .load(imageUri)
    .asBitmap()
    .override(newWidth, newHeight)
    .transform(new CustomFitCenter(this))
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            Log.e(TAG, "Loaded Bitmap Size :" + resource.getWidth() + "x" + resource.getHeight());
            /*
            .
            . Initialize GLSurfaceView with Bitmap resource
            .
            */
        }
    }) ;

I'm not sure if it's the best way to do it, but it worked.


This answer was posted as an edit to the question Android - Glide resizing image via "override" and "fitCenter" deforms Bitmap by the OP Zac Jokface under CC BY-SA 3.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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