You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使绘制的Bitmap与DrawCircle的尺寸精准匹配?

如何让位图与圆的尺寸正确匹配缩放?

我正在做学校项目的碰撞检测代码,发现用canvas.DrawBitmap(currentImage, position.X, position.Y, paint)绘制位图时,它的尺寸和圆的半径不匹配,图像看起来比实际的圆大很多。但用canvas.DrawCircle(position.X, position.Y, size.Width / 2, new Paint() { Color = Color.Black })绘制圆时,缩放是完全正确的。请问怎么才能创建一个和实际圆尺寸正确缩放的位图?

相关代码:

public Sprite(float x, float y, float size, Context context) : base(x, y)
{
    int id = (int)typeof(Resource.Drawable).GetField(General.IMG_BALL).GetValue(null);
    image = BitmapFactory.DecodeResource(context.Resources, id);
    this.size = new SizeF(size, size);
    paint = new Paint(PaintFlags.AntiAlias);
    paint.AntiAlias = false;
    paint.FilterBitmap = false;
    paint.Dither = true;
    paint.SetStyle(Paint.Style.Fill);
}

public void DisplayImage(Canvas c)
{
    Matrix matrix = new Matrix();
    matrix.PostTranslate(position.X, position.Y);
    matrix.PreScale(size.Width, size.Height);
    c.DrawBitmap(image, matrix, paint);
}

绘制结果:

  • 使用canvas.DrawCircle(position.X, position.Y, size.Width / 2, new Paint() { Color = Color.Black })(缩放正确,无位图,半径=100):
    [缩放正确的圆截图]
  • 使用自定义的DisplayImage(canvas)函数(缩放错误,使用位图,半径=1以适配图像):
    [缩放错误的位图截图]

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 11:55:05