[英]Draw smoothly scaled bitmaps on Canvas
This is how I draw Bitmap
on Canvas
in my Android app:这就是我在 Android 应用程序中在
Canvas
上绘制Bitmap
:
canvas.save();
canvas.scale(scale, scale, x, y);
canvas.drawBitmap(bitmap, x, y, null);
canvas.restore();
However the Bitmap
is not scaled smoothly, no anti-aliasing is performed.但是
Bitmap
没有平滑缩放,没有执行抗锯齿。 How can I enable anti-aliasing?如何启用抗锯齿?
Try this:尝试这个:
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(bitmap, x, y, paint);
Both Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
两者
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
or paint.setFilterBitmap(true);
或
paint.setFilterBitmap(true);
worked for me but be very careful, on my game it cut down the FPS from 30FPS
to 17FPS
only.对我
17FPS
,但要非常小心,在我的游戏中,它仅将 FPS 从30FPS
到17FPS
。 So if it is a mission critical drawing like in a game you better scale the image at loading time.因此,如果它是游戏中的关键任务绘图,则最好在加载时缩放图像。 Which I did in the following manner:
我以下列方式做的:
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;
}
Have you tried creating a Paint
object, calling setAntiAlias(true)
on it and passing it to the drawBitmap
method as the 4th parameter?您是否尝试过创建
Paint
对象,对其调用setAntiAlias(true)
并将其作为第四个参数传递给drawBitmap
方法? If this does not work I guess you should scale down the drawBitmap
call instead of scaling the Canvas, eg by using drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
.如果这不起作用,我想您应该缩小
drawBitmap
调用而不是缩放画布,例如通过使用drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
。
You need only one Line of Code:您只需要一行代码:
canvas.drawBitmap(bitmap, x, y, new Paint(Paint.ANTI_ALIAS_FLAG));
Not 5 Lines不是 5 行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.