2017-09-16 88 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

正如你可以看到我编辑我使用过。 –