这就是我Bitmap
在Canvas
我的 Android 应用程序中使用的方式:
canvas.save();
canvas.scale(scale, scale, x, y);
canvas.drawBitmap(bitmap, x, y, null);
canvas.restore();
但是,Bitmap
缩放不平滑,不执行抗锯齿。如何启用抗锯齿?
这就是我Bitmap
在Canvas
我的 Android 应用程序中使用的方式:
canvas.save();
canvas.scale(scale, scale, x, y);
canvas.drawBitmap(bitmap, x, y, null);
canvas.restore();
但是,Bitmap
缩放不平滑,不执行抗锯齿。如何启用抗锯齿?
尝试这个:
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);
为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;
}
您是否尝试过创建一个Paint
对象,调用它并将其作为第四个参数setAntiAlias(true)
传递给方法?drawBitmap
如果这不起作用,我猜你应该缩小drawBitmap
调用而不是缩放画布,例如使用drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
.
您只需要一行代码:
canvas.drawBitmap(bitmap, x, y, new Paint(Paint.ANTI_ALIAS_FLAG));
不是 5 行