lopspower / CircularImageView

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

Image scaled not cut out #25

Closed Dustin1358 closed 9 years ago

Dustin1358 commented 9 years ago

If you dont want have the image scaled like now you can edit the draw() method like this. It will allways cut out the middle of the image, I hope this helps you! And sorry for my bad english.

public void onDraw(Canvas canvas) {
    // load the bitmap
    image = drawableToBitmap(getDrawable());

    // init shader
    if (image != null) {

        canvasSize = canvas.getWidth();
        if(canvas.getHeight()<canvasSize)
            canvasSize = canvas.getHeight();

        //Added by Dustin1358
        int bitmapwidth=0;
        int bitmapheigth=0;
        int bitmapsize= image.getWidth();
        if(image.getHeight()<bitmapsize){
            bitmapsize = image.getHeight();
            bitmapwidth=(image.getWidth()-bitmapsize)/2;
        }else{
            bitmapheigth=(image.getHeight()-bitmapsize)/2;
        }
        Bitmap bmp=Bitmap.createBitmap(image,bitmapwidth,bitmapheigth, bitmapsize, bitmapsize);
        BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(bmp, canvasSize, canvasSize, false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        //Original
                    //BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvasSize, canvasSize, false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        paint.setShader(shader);

        // 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
        int circleCenter = (canvasSize - (borderWidth * 2)) / 2;
        canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) + borderWidth - 4.0f, paintBorder);
        canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint);
    }
lopspower commented 9 years ago

Thank you for you contribution :)