【问题标题】:Draw smoothly scaled bitmaps on Canvas在 Canvas 上绘制平滑缩放的位图
【发布时间】:2011-05-16 16:45:28
【问题描述】:

这就是我在我的 Android 应用中在Canvas 上绘制Bitmap 的方式:

canvas.save();
canvas.scale(scale, scale, x, y);
canvas.drawBitmap(bitmap, x, y, null);
canvas.restore();

但是Bitmap 没有平滑缩放,没有执行抗锯齿。如何启用抗锯齿功能?

【问题讨论】:

  • 只是一个注释。如果您只想要一个方形结果,无论您需要放大还是缩小,都可以使用这个非常方便的技巧...... stackoverflow.com/a/17733530/294884 希望它可以帮助某人

标签: android android-canvas antialiasing


【解决方案1】:

试试这个:

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);

canvas.drawBitmap(bitmap, x, y, paint);

【讨论】:

  • 谢谢,成功了,我刚用Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
  • 我对此并不满意。你是放大还是缩小图像?
  • 我只是想我会说在 setAntiAlias(true) 之外添加 setFilterBitmap(true) 和 setDither(true) 会对 Wear 设备产生巨大影响。
  • 工作正常!谢谢!
【解决方案2】:

Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);paint.setFilterBitmap(true); 都为我工作,但要非常小心,在我的游戏中,它仅将 FPS 从 30FPS 减少到 17FPS。因此,如果它是游戏中的关键任务绘图,您最好在加载时缩放图像。我以以下方式做到了:

public Bitmap getImage (int id, int width, int height) {
    Bitmap bmp = BitmapFactory.decodeResource( getResources(), id );
    Bitmap img = Bitmap.createScaledBitmap( bmp, width, height, true );
    bmp.recycle();
    return img;
}

【讨论】:

  • createScaledBitmap 是唯一能为我产生平滑边缘的解决方案。谢谢!
【解决方案3】:

您是否尝试过创建Paint 对象,在其上调用setAntiAlias(true) 并将其作为第四个参数传递给drawBitmap 方法?如果这不起作用,我想您应该缩小drawBitmap 调用而不是缩放画布,例如通过使用drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)

【讨论】:

    【解决方案4】:

    你只需要一行代码:

    canvas.drawBitmap(bitmap, x, y, new Paint(Paint.ANTI_ALIAS_FLAG)); 
    

    不是 5 行

    【讨论】:

    • 请不要只是添加代码,还要提供一些解释。
    • @Rohan:问题是:“在画布上绘制平滑缩放的位图”然后你可以使用例如canvas.drawBitmap(source, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));为了这。你现在明白了吗?
    • @Rohan:另一方面:我为什么要在这里提供冗余代码?请先阅读其他答案。
    • 你误会我了。我只是希望您添加一些解释,这将有助于很多新手,例如 -- 使用Paint.ANTI_ALIAS_FLAG 标志,该标志用于去除锯齿状边缘并平滑边缘。像这样的东西。 (然后你也添加代码)
    • 如果你看不懂这段代码,你必须训练。但这不是反对投票的理由
    猜你喜欢
    • 2013-01-06
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多