OnDraw : Canvas 未绘制位图

OnDraw : Canvas is not drawing bitmap

我正在使用 Xamarin.Android 制作自定义视图,我想在 canvas 中绘制一个圆然后在其上绘制位图。圆正在正确绘制,但位图不是 位图对应一个png图片资源。 这是我的 OnDraw

    protected async override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);
        Paint buttonBackgroundPaint = new Paint(PaintFlags.AntiAlias);
        Color? c = null;
        bool colorOk = Resources.TryGetColor(_backgroundColor, out c);

        buttonBackgroundPaint.Color = (Color)(colorOk ? c : _context?.Resources.GetColor(AndroidRes.Resource.Color.Black));
        canvas.DrawCircle(Width / 2, Height / 2, _radius, buttonBackgroundPaint);

        Paint bitmapPaint = new Paint(PaintFlags.AntiAlias);
        Bitmap bitmapContent = await BitmapFactory.DecodeResourceAsync(_context?.Resources, _drawableContent);
        canvas.DrawBitmap(bitmapContent, 0, 0, bitmapPaint);

    }

编辑:Resources.TryGetColor 是我创建的扩展方法。

我注意到旋转屏幕时会显示部分位图。 问题出在位图边界上,所以我按如下方式解决了它:

protected async override void OnDraw(Canvas canvas)
{
    base.OnDraw(canvas);
    Paint buttonBackgroundPaint = new Paint(PaintFlags.AntiAlias);
    Color? c = null;
    bool colorOk = Resources.TryGetColor(_backgroundColor, out c);

    buttonBackgroundPaint.Color = (Color)(colorOk ? c : _context?.Resources.GetColor(AndroidRes.Resource.Color.Black));
    canvas.DrawCircle(Width / 2, Height / 2, _radius, buttonBackgroundPaint);

    Drawable dr = _context.Resources.GetDrawable(Resource.Drawable.image);
    dr.SetBounds(0, 0, right, bottom); // appropiate values for my case
    dr.Draw(canvas);
}