Skip to content

Instantly share code, notes, and snippets.

@plateaukao
Created April 4, 2019 07:09
Show Gist options
  • Save plateaukao/65e2f2e1650385c2a9ecaebeb5c5b476 to your computer and use it in GitHub Desktop.
Save plateaukao/65e2f2e1650385c2a9ecaebeb5c5b476 to your computer and use it in GitHub Desktop.
Show image in circle in Android
package com.linecorp.linesdk.dialog.internal;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
public class CircleImageView extends AppCompatImageView {
public CircleImageView(Context context) {
super(context);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setImageDrawable(@Nullable Drawable drawable) {
if (drawable == null) {
super.setImageDrawable(null);
return;
}
Bitmap imageBitmap = ((BitmapDrawable) drawable).getBitmap();
RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), imageBitmap);
imageDrawable.setCircular(true);
imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
super.setImageDrawable(imageDrawable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment