Take a look at the unprocessed rectangular head, which appears a little stiff and incongruous, and then take a look at the round head, is it a sense of beauty double ah.

So how can you achieve a wide variety of image styles? To this question, our sweet little cotton-padded Picasso provided Transformation with a very useful interface. That being the case, it’s easy to make a round avatar.

The implementation procedure is as follows: 1. Define a circular avatar inheritance and Transformation interface

Class CircleTransform implements Transformation{/** * @param source: an unprocessed rectangular Bitmap object */ @override public Bitmap transform(Bitmap source) {return null; } /** * This method has no real meaning, but it must not return null! * @return */ @Override public String key() { return ""; }}Copy the code

2. Write a BitmapUtils utility class that implements two methods:

① Method of compression processing for bitmap

② The method of converting rectangular Bitmap object into circular Bitmap

Schematic diagram:

The code is as follows (with a slight understanding of functional code) :

Public class BitmapUtils {/** convert rectangle Bitmap to round Bitmap * @param source: rectangle Bitmap to be processed * @return: Public static Bitmap circleBitmap(Bitmap source){int width = source.getwidth (); Bitmap Bitmap = bitmap.createBitmap (width, width, bitmap.config. ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint = new Paint(); paint.setAntiAlias(true); DrawCircle (width / 2, width / 2, width / 2, width / 2, paint); //setXfermode: // Porterduff.mode.SRC_IN takes the intersection section of two layers of images, and only displays the upper image, note that here refers to the intersection part, and then displays the upper image SetXfermode (new PorterDuffXfermode(porterduff.mode.src_in)); Draw a bitmap on the canvas. DrawBitmap (source, 0, 0, paint); return bitmap; } /** Compress bitmap * @param source: Bitmap to be processed * @param width must be compressed to float * @param height must be compressed to float * @return returns the compressed Bitmap * Note! Parameter 2,3 must be supplied as a floating point. */ public static Bitmap zoom(Bitmap source,float width,float height){ Matrix matrix = new Matrix(); float scaleX = width / source.getWidth(); float scaleY = height / source.getHeight(); matrix.postScale(scaleX, scaleY); Bitmap Bitmap Bitmap = createBitmap (source, 0, 0, source getWidth (), the source, getHeight (), matrix, true); return bitmap; }}Copy the code

3. Call methods in utility classes: Perfect the custom CircleTransform class

Class CircleTransform implements Transformation{/** * @param source: unprocessed Bitmap * @return: */ @override public Bitmap transform(Bitmap source) { Zoom (source, jmuiutils.dp2px (62), jmuiutils.dp2px (62)); zoomBitmp = bitmaputils.zoom (source, jmuiutils.dp2px (62)); Bitmap = BitmapUtils. CircleBitmap (zoomBitmp); Source.recycle (); return bitmap; } /** * This method has no practical significance, but it must not return the value of null! * @return */ @Override public String key() { return ""; }}Copy the code

4. Call the transform method and pass in the CircleTransform object

Picasso.with(getActivity()).load(user.UF_AVATAR_URL).transform(new CircleTransform()).into(imageView1);
Copy the code

End result: