lopspower / CircularImageView

Create circular ImageView in Android in the simplest way possible
Apache License 2.0
1.95k stars 413 forks source link

Images that are not squares lost his original aspect ratio #6

Closed jzafrilla closed 9 years ago

jzafrilla commented 10 years ago

If you use this library with images 4:3 / 16:9 / 9:16 / it doesn't respect de aspect ratio. If you use 1:1 images, works perfectly

ClemMahe commented 10 years ago

Hi jzafrilla, i found one very simple way to fix this (API 8+). Thanks to this article on stackoverflow : http://stackoverflow.com/questions/6908604/android-crop-center-of-bitmap

In CircularImaveView.java, around L80, just use ThumbnailUtils.extractThumbnail with canvasSize.

canvasSize = canvas.getWidth();
if(canvas.getHeight()<canvasSize) canvasSize = canvas.getHeight();      
BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(ThumbnailUtils.extractThumbnail(image, canvasSize, canvasSize), canvasSize, canvasSize, false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
jzafrilla commented 10 years ago

Thanks for your response R3n4rD, it works perfect!!, final solution

if (image != null) {
            int canvasSize = canvas.getWidth();
            if(canvas.getHeight()<canvasSize){
                canvasSize = canvas.getHeight();      
            }
            BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(
                    ThumbnailUtils.extractThumbnail(image, canvasSize,
                            canvasSize), canvasSize, canvasSize, false),
                    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
            //shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
            int circleCenter = viewWidth / 2;
            // circleCenter is the x or y of the view's center
            // radius is the radius in pixels of the cirle to be drawn
            // paint contains the shader that will texture the shape
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);
        }
lopspower commented 9 years ago

Thank you for your contributions guys. I add your update :)