2017-09-16 93 views
-1

我正在製作android的像素遊戲。我正在使用32x32圖像。爲了讓遊戲看起來一樣,無論屏幕尺寸如何,我都會動態地放大圖像。我的問題是,放大的時候,圖像的部分不保持其原有的顏色:完美地放大小位圖像素

6 tiles, just before the black edges there is an unwanted shadowy line (presumably the average of the black and the redish color)

6個塊,32×32最初。正如你所看到的,在黑色邊緣之前有一個不需要的陰影線(大概是黑色和紅色的平均值)。

這是我使用縮放代碼:

public abstract class Drawable { 
    protected int x; 
    protected int y; 
    protected Bitmap image; 

    Drawable(Bitmap image, int x, int y, float scale) { 
     this.x = x; 
     this.y = y; 
     this.image = Bitmap.createScaledBitmap(image, (int)(image.getWidth()*scale), (int)(image.getHeight()*scale), false); 
    } 

    abstract void draw(Canvas canvas); 
} 

正如你看到的,我不使用過濾器。這會使邊緣區域更模糊。是否有另一個過濾器,相比之下,如果在放大時真正使用哪個真正保持圖像的清晰度?

編輯:

現在我試過這種方法代替:

scaledRect = new RectF(x, y, x+image.getWidth()*scale, y+image.getHeight()*scale); 
    paint = new Paint(); 
    paint.setAntiAlias(false); 
    paint.setDither(false); 
    paint.setFilterBitmap(false); 

而且在繪製調用:

canvas.drawBitmap(this.image, null, scaledRect, paint); 

沒有成功...

+0

向上擴展位圖將有模糊的副作用。也許矢量圖形是你正在尋找的。 https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html –

+0

我從來沒有見過任何人使用矢量圖形制作像素藝術,但我會檢查出來。 –

回答

1

的Android處理位圖使用雙線性插值算法默認縮放。你想要做的是最近鄰插值。

做一個Paint,關閉抖動和抗混疊,不要createScaledBitmap借鑑和嘗試這個辦法:

paint.setDither(false); 
paint.setAntiAlias(false); 

canvas.drawBitmap(bitmap, null, new RectF(left, top, width, height), paint); 
+0

謝謝,但我仍然得到同樣的問題... –

+0

嘗試添加'paint.setFilterBitmap(false);'以及 – shiftpsh

+0

正如你可以看到我編輯我使用過。 –