Leave photo rounded equal Whatsapp

Asked

Viewed 1,667 times

2

I started a short time in the development area for Android.

I would like to know what feature I use, in XML, to leave a rounded photo equal to a Whatsapp contact.

I already use for some images the rounded border, creating a XML drawable with the shape. But I don’t think that’s the case.

  • I use the bootstrap lib for Android. Here is the lib with the examples below: https://github.com/Bearded-Hen/Android-Bootstrap

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 makes a layerlist drawable:

// 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 as background to Imageview:

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

You’ll get something like this:

inserir a descrição da imagem aqui

  • In case I don’t want to take a specific image, but a photo taken from the app and then add to the contact in a Listview. That is, how I will assign the img.xml item ?

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,example:

Glide.with(ivUser.getContext()).load(urlPhotoUser).centerCrop().transform(new CircleTransform(ivUser.getContext())).override(40,40).into(ivUser);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.