Leave a round photo like whatsapp

2

I started shortly in the Android development area.

I'd like to know what feature I use in XML to make a rounded photo equal to a Whatsapp contact.

I already use the rounded border for some images, creating a XML drawable with shape . But I do not think that's the case.

    
asked by anonymous 14.05.2015 / 16:35

2 answers

2

You can make a simple circle with white border and transparent content:

// res/drawable/circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1.9"
    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="10dp"
        android:color="@android:color/white" />
</shape>

Then make a drawable layerlist:

// res/drawable/img.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/ic_launcher"/>
    <item android:drawable="@drawable/circle"/>

</layer-list>

And then put it as a backdrop for ImageView:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/img"/>

It will look something like this:

    
14.05.2015 / 17:09
0

You can use this or

public class CircleTransform extends BitmapTransformation {

public CircleTransform(Context context) {
    super(context);
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    if (toTransform == null) return null;

    int size = Math.min(toTransform.getWidth(), toTransform.getHeight());
    int x = (toTransform.getWidth() - size) / 2;
    int y = (toTransform.getHeight() - size) / 2;

    // TODO this could be acquired from the pool too
    Bitmap squared = Bitmap.createBitmap(toTransform, x, y, size, size);

    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    return result;
}

@Override
public String getId() {
    return  getClass().getName();
}

}

ai recover in the activity you want, eg:

Glide.with(ivUser.getContext()).load(urlPhotoUser).centerCrop().transform(new CircleTransform(ivUser.getContext())).override(40,40).into(ivUser);
    
01.07.2017 / 20:14