2010-11-27 161 views
27

顺利缩放位图这是我在我的Android应用程序上CanvasBitmap在画布上绘制

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

然而Bitmap不顺利缩放,不执行抗锯齿。我如何启用抗锯齿?

+1

刚一说明。如果你只需要一个平方的结果,无论你需要放大或缩小,使用这个令人难以置信的方便的技巧... stackoverflow.com/a/17733530/294884希望它可以帮助别人 – Fattie 2014-06-02 13:43:45

回答

68

试试这个:

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

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

谢谢,它的工作,我只是用`油漆涂料=新油漆(Paint.FILTER_BITMAP_FLAG);` – fhucho 2010-11-28 09:58:33

+4

我对此没有任何喜悦。你是在缩放你的图片吗? – teedyay 2011-01-10 12:46:39

+0

我只是认为我会在添加setFilterBitmap(true)和setDither(true)INAUTITION到setAntiAlias(true)时在Wear设备上产生差异。 – 2015-07-22 21:36:10

16

两个Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);paint.setFilterBitmap(true);为我工作,但要非常小心,我的比赛就砍下了FPS从30FPS17FPS。因此,如果它是像游戏一样的任务关键绘图,那么您最好在加载时缩放图像。我照做了以下方式:

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; 
} 
0

用途:

canvas.drawBitmap(source, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));