I have the following piece of code for rendering an imageView with rounded corners.
public class RoundedCornerImageView extends ImageView {
private int rounded;
public RoundedCornerImageView(Context context) {
super(context);
}
public RoundedCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedCornerImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public int getRounded() {
return rounded;
}
public void setRounded(int rounded) {
this.rounded = rounded;
}
@Override
public void onDraw(Canvas canvas)
{
Drawable drawable = getDrawable();
int w = drawable.getIntrinsicHeight(),
h = drawable.getIntrinsicWidth();
Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvas tmpCanvas = new Canvas(rounder);
// We're going to apply this paint eventually using a porter-duff xfer mode.
// This will allow us to only overwrite certain pixels. RED is arbitrary. This
// could be any color that was fully opaque (alpha = 255)
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.WHITE);
// We're just reusing xferPaint to paint a normal looking rounded box, the 20.f
// is the amount we're rounding by.
tmpCanvas.drawRoundRect(new RectF(0,0,w,h), 10.0f, 10.0f, xferPaint);
// Now we apply the 'magic sauce' to the paint
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
drawable.draw(canvas);
canvas.drawBitmap(rounder, 0, 0, xferPaint);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background='#a3deef'
>
<com.example.scheduling_android.view.RoundedCornerImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/eventImageView"
android:adjustViewBounds="false"/>
</LinearLayout>
It works in that it is indeed cropping off the corners of the image. However, the problem arises when I try to render this inside a linearLayout that has a background color #a3deef. The resulting display is a background color of #a3deef with each image displayed with rounded corners where the 4 cropped corner are all in black.
What should I do to make the cropped corners transparent rather than black? Also, it would be great if someone could explain to me why it would black, and not any other colors too!
Thanks in advance.