位图缩放破坏质量

Bitmap scale destroy quality

我已经为 Android Wear 制作了一个表盘。
但问题是图像缩放。当我调整它们的大小时,背景、标记和小工具的图像质量下降。

例如,我将 480x480 的背景图像放在 drawable-nodpi 文件夹中(我也尝试过其他 dpi),然后像这样重新缩放它:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
Bitmap bgImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ticks_1, options);
bgImage = Bitmap.createScaledBitmap(bgImage , width, height, false);

图片如下:

这就是我在手表上看到的:

我是不是在调整大小方面做错了什么?

对以下代码的一些评论。解码的选项部分没有任何作用,因为没有设置样本大小。所以可以去掉。

// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inScaled = true;
// Bitmap bgImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ticks_1, options);
Bitmap bgImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ticks_1);
bgImage = Bitmap.createScaledBitmap(bgImage , width, height, false);

我认为您的图像失真问题实际上是图像本身。当你调整大小时下降到 75%。它只是无法在保持图像看起来完全相同的同时调整它的大小。条带非常细,任何轻微的偏移都会导致看起来不同。

我建议尝试使用更厚的戒指。并查看图像是否失真。甚至只是作为快速测试或永久解决方案。

更新:

要进一步了解如何在所有分辨率下保持图像清晰,请查看 VECTOR 图像。这些在所有分辨率下都是完美的像素,并且会在所有手表上呈现出色的图形。特别是,因为你使用的是基本形状,应该很容易实现。

如果您正在调整位图的大小,您可能会在重新绘制图像时出现模糊。 如果它必须是位图,请在不同的 dpi 文件夹中包含多个分辨率,以便 android 相应地选择最适合调整大小的分辨率。

改变

bgImage = Bitmap.createScaledBitmap(bgImage , width, height, false);

bgImage = Bitmap.createScaledBitmap(bgImage , width, height, true);

这将应用简单的双线性插值。由于圆圈太细,结果会有点模糊,但会有更多信息用于创建缩放图像。结果图像也会有一些波纹图案,但会比示例图片中的失真少。如果你在 Photoshop 中使用双三次过滤缩小图像,你会得到最好的结果,但即便如此,使用矢量图形也会胜过任何基于位图的努力,比如 Doomsknight 建议的。

我找到了一个解决方案 here 并且效果很好。
这是缩放图像的代码部分:

public Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        Bitmap resizedBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

        float scaleX = newWidth / (float) bitmap.getWidth();
        float scaleY = newHeight / (float) bitmap.getHeight();
        float pivotX = 0;
        float pivotY = 0;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

        Canvas canvas = new Canvas(resizedBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));

        return resizedBitmap;
    }

以上采纳切位图试试我的回答

  public Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) {
             Bitmap scaledBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(resizedBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));

    return resizedBitmap;
}