1

I have a ImageView in Xamarin which has rounded corners. My code worked fine for Android 4.4 and below but Lollipop onward it now has a black background added to it Black Area should be Transparent - https://i.stack.imgur.com/GRFnl.png

    public class RoundedImageView : ImageView
{
    private int _cornerRadiusPercentage;
    private Path _path = null;

    public RoundedImageView(Context context, IAttributeSet attrs)
        : base(context, attrs)
    {
        this._cornerRadiusPercentage = attrs.GetAttributeIntValue("http://schemas.boomo.android/roundedImageView", "corner_radius_percentage", 10);

        this.SetLayerType(LayerType.Software, null);
    }

    public RoundedImageView(Activity activity, int cornerRadiusPercentage)
        : base(activity)
    {
        this._cornerRadiusPercentage = cornerRadiusPercentage;
        this.SetLayerType(LayerType.Software, null);
    }

    private void SetPath(Canvas canvas)
    {
        int offsetLeft = this.PaddingLeft;
        int offsetRight = this.PaddingRight;
        int offsetTop = this.PaddingTop;
        int offsetBottom = this.PaddingBottom;

        int cw = canvas.Width - offsetLeft - offsetRight;
        int ch = canvas.Height - offsetTop - offsetBottom;

        int cornerRadius = (int)((this._cornerRadiusPercentage / 100f) * cw);

        _path = new Path();
        _path.AddRoundRect(new RectF(offsetLeft, offsetTop, cw, ch), cornerRadius, cornerRadius, Path.Direction.Cw);
        _path.Close();
    }

    protected override void OnDraw(Canvas canvas)
    {
        //Paint paint = new Paint();
        //paint.Color = Color.Transparent;
        //paint.Alpha = 254;
        //paint.AntiAlias = true;

        if (_path == null)
            SetPath(canvas);
        canvas.ClipPath(_path);
        //canvas.DrawPath(_path, paint);
        base.OnDraw(canvas);
    }
}

*Note The commented out sections are bits I've tried to use to fix the issue

blackasninja
  • 11
  • 1
  • 4
  • just use android.support.v4.graphics.drawable.RoundedBitmapDrawable – pskink Jul 01 '15 at 04:15
  • Im using UrlImageViewHelper to help me load the images from service and display loading images. Im not sure if a bitmap can be passed in. – blackasninja Jul 02 '15 at 06:19
  • see https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawableFactory.html#create(android.content.res.Resources, android.graphics.Bitmap) – pskink Jul 02 '15 at 06:24
  • Solved it using this method instead - http://stackoverflow.com/a/24717337/3163946 – blackasninja Jul 03 '15 at 04:07

0 Answers0