2010-11-27 286 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));