Picasso via URL-a circular display of the user's avatar

Source: Internet
Author: User

1. Set Layout properties:

<imageview    android:scaletype= "Fitxy"/>

2.BitmapUtils class--bitmap object with a specified circle

public static Bitmap Circlebitmap (Bitmap source) {    //Gets the width of the Bitmap    int width = source.getwidth ();    The width value of the bitmap as the new bitmap's wide height value.    Bitmap Bitmap = Bitmap.createbitmap (width, width, Bitmap.Config.ARGB_8888);    Using this bitmap as a benchmark, create a canvases canvas canvas    = new canvas (bitmap);    Paint paint = new paint ();    Paint.setantialias (true);    Draw a circle canvas.drawcircle on the canvas    (WIDTH/2, WIDTH/2, WIDTH/2, paint);    To set how images are handled when the picture intersects    //setxfermode: Sets the mode of processing when the image being drawn intersects, it contains the common patterns:    //porterduff.mode.src_in Take two layers of image intersection part, Show only the upper image    //porterduff.mode.dst_in take the two-layer image intersection section, showing only the underlying image    Paint.setxfermode (New Porterduffxfermode ( PorterDuff.Mode.SRC_IN));    Draw Bitmap    canvas.drawbitmap (source, 0, 0, paint) on the canvas;    return bitmap;}

3.BitmapUtils class--Compress picture

Implementation of image compression processing//Setting wide height must use floating-point type, otherwise result in compression ratio: 0public static Bitmap Zoom (Bitmap source,float width, float height) {    Matrix Matrix = new Matrix ();    Image compression processing    Matrix.postscale (Width/source.getwidth (), Height/source.getheight ());    Bitmap Bitmap = bitmap.createbitmap (source, 0, 0, source.getwidth (), Source.getheight (), Matrix, false);    return bitmap;}

4. Display the circular image according to User.getimageurl ()

Get Picture Picasso.with (This.getactivity ()) using Picasso networking. Load (User.getimageurl ()). Transform (New transformation () {    @Override public    Bitmap transform (Bitmap source) {//Download Bitmap object in memory after//        compression processing        Bitmap Bitmap = Bitmaputils.zoom (source, uiutils.dp2px), uiutils.dp2px (());        Round treatment        bitmap = Bitmaputils.circlebitmap (bitmap);        Recycling Bitmap Resources        source.recycle ();        return bitmap;    }    @Override Public    String key () {        return "";//need to guarantee that the return value cannot be null. Otherwise the error    }). into (Ivmeicon);

  

  

  

Picasso via URL-a circular display of the user's avatar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.