android avatar circle display

public Bitmap toRoundBitmap(Bitmap bitmap) {  

        //Circular image width and height  

        int width = bitmap.getWidth();  

        int height = bitmap.getHeight();  

        // side length of the square  

        int r = 0;  

        // Take the shortest side as the side length  

        if(width > height) {  

            r = height;  

        } else {  

            r = width;  

        }  

        //build a bitmap  

        Bitmap backgroundBmp = Bitmap.createBitmap(width,  

                 height, Config.ARGB_8888);  

        //new a Canvas, draw on backgroundBmp  

        Canvas canvas = new Canvas(backgroundBmp);  

        Paint paint = new Paint();  

        //Set the edges to be smooth and remove jagged edges  

        paint.setAntiAlias(true);  

        //Equal width and height, that is, a square  

        RectF rect = new RectF(0, 0, r, r);  

        //Draw a rounded rectangle through the specified rect. When the radius of the rounded corner in the X-axis direction is equal to the radius in the Y-axis direction,  

        //When both are equal to r/2, the rounded rectangle drawn is a circle  

        canvas.drawRoundRect(rect, r/2, r/2, paint);  

        //Set the mode when two graphics intersect, SRC_IN is to take the intersecting part of the SRC graphics, the excess will be removed  

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  

        //canvas draws bitmap on backgroundBmp  

        canvas.drawBitmap(bitmap, null, rect, paint);  

        //Return the backgroundBmp that has been painted  

        return backgroundBmp;  

    } 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326970583&siteId=291194637