Android - Resizing an image with override and fitCenter warps a bitmap

I use Glide to upload images from the gallery to GLSurfaceView . However, when I tried to resize the image using override(width, height) , it does not. So I added fitCenter() , which seems to be the key to the desired size.

My problem is that when resizing Bitmap result is rather strange! All is well, except for images with small widths. The fitCenter() figures illustrate the difference between using and using fitCenter() .

Here is the code I used to download through 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 it might be a GLSurfaceView problem, something related to the very small width that I guessed about. But it looks like it displays fine until the image is resized.

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

enter image description here

EDIT [SOLVED]:
It seems like the problem occurs when using fitCenter() with GLSurfaceView with a Bitmap that has width or height as an odd number. I solved the problem by adding the following custom conversion to a 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 causes 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 this is the best way to do this, but it worked. Hope this helps someone.

+5
source share

All Articles