我正在制作一款基于 Pixel 的 Android 游戏。我有几个 8x8 的精灵需要调整大小以适应 100x100 的区域。作为测试是否可行,我尝试让图像填满整个画布。它有点工作,但它使 8x8 精灵变成了 12x12 精灵,使像素看起来非常奇怪和扭曲。这是我到目前为止所拥有的:
Bitmap grass = BitmapFactory.decodeResource(getResources(), R.drawable.small);
Paint configuration = new Paint();
configuration.setDither(false);
configuration.setAntiAlias(false);
Matrix myMatrix = new Matrix();
myMatrix.postScale(canvas.getWidth() / grass.getWidth(), canvas.getWidth() / grass.getHeight());
Bitmap resizedBitmap = Bitmap.createBitmap(grass, 0, 0, grass.getWidth(), grass.getHeight(), myMatrix, false);
canvas.drawBitmap(resizedBitmap, 0, 0, null);