I have created a round imageView based on this answer. It makes image round perfectly. However, I have two problems.
- Image rotated 90 degree that I have no idea why (user clicks on a button, user's gallery displays, user selects an image as his profile pic)
- image has dark background that I have no idea comes from where.
The class that I'm using:
public class RoundImageView extends ImageView {
private Path path;
private Paint paint;
private PorterDuffXfermode porterDuffXfermode;
public RoundImageView(Context context) {
super(context);
init();
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setWillNotDraw(false);
path = new Path();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
// Create a circular path.
final float halfWidth = canvas.getWidth()/2;
final float halfHeight = canvas.getHeight()/2;
final float radius = Math.max(halfWidth, halfHeight);
path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);
paint.setXfermode(porterDuffXfermode);
canvas.drawPath(path, paint);
}
}
The way I'm adding it in layout:
<com.allstarxi.widget.RoundImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/ic_launcher"
android:id="@+id/imageView"
android:contentDescription="@string/general_content_description"
android:scaleType="center" />
This is screenshot: