简体   繁体   English

位图比例破坏质量

[英]Bitmap scale destroy quality

I have made a Watch Face for Android Wear.我为 Android Wear 制作了一个表盘。
But the problem is image scaling.但问题是图像缩放。 Images for background, markers and gadgets are of reduced quality when I resize them.当我调整它们的大小时,背景、标记和小工具的图像质量会降低。

For example, I put 480x480 background image in drawable-nodpi folder (I have tried other dpi as well), and I rescale it like this:例如,我将 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);

Here are the images:以下是图片:

背景 在此处输入图片说明

and this is what I get on the watch:这就是我在手表上看到的:

截图

Am I doing something wrong with resizing or what?我在调整大小或什么方面做错了什么?

I have found a solution here and it works great.在这里找到了一个解决方案,效果很好。
Here is the part of the code for scaling the image:这是用于缩放图像的代码部分:

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;
    }

A few comments on the below code.对以下代码的一些评论。 The options section of your decoding doesn't do anything, as no sample size is set.解码的选项部分没有任何作用,因为没有设置样本大小。 So it can be removed.所以可以去掉。

// 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);

I think the issue with your image distortion, is in-fact the image itself.我认为您的图像失真问题实际上是图像本身。 When you resize is down to 75%.当你调整大小是下降到 75%。 It is just unable to resize it while keeping the image looking exactly the same.它只是无法在保持图像看起来完全相同的同时调整其大小。 the bands are so thin that any slight off is causing to appear different.带子很薄,任何轻微的脱落都会导致看起来不同。

I suggest trying it with thicker rings.我建议尝试使用较厚的戒指。 And see if the image is distorted or not.并查看图像是否失真。 Even just as a quick test, or permanent solution.即使只是作为快速测试或永久解决方案。

update:更新:

To expand on keeping images crisp clear at all resolutions, look into VECTOR images .要扩展在所有分辨率下保持图像清晰,请查看矢量图像 These are pixel perfect at all resolutions, and would present fantastic graphics on all watches.这些在所有分辨率下都是像素完美的,并且可以在所有手表上呈现出色的图形。 Especially, since you are using basic shapes, it should be easy to achieve.特别是,由于您使用的是基本形状,因此应该很容易实现。

If you are resizing bitmaps, you can expect blur as the image is redrawn.如果您正在调整位图的大小,则在重新绘制图像时可能会出现模糊。 If it must be a bitmap, include multiple resolutions in the different dpi folders so that android selects accordingly, the best match for resizing.如果它必须是位图,请在不同的 dpi 文件夹中包含多个分辨率,以便 android 相应地选择调整大小的最佳匹配。

Change改变

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

to

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

This will apply simple bilinear interpolation.这将应用简单的双线性插值。 Result will be a bit blurry image as circles are so thin, but there will be more information used to create scaled image.由于圆圈太薄,结果会有点模糊,但会有更多信息用于创建缩放图像。 Resulted image will also have some moire-pattern, but will be less distorted than in your example picture.生成的图像也会有一些莫尔图案,但不会像示例图片那样失真。 If you downscale image in Photoshop using Bicubic filtering, you will get the best results, but even then using vector graphics will outperform any bitmap-based efforts like Doomsknight suggested.如果您在 Photoshop 中使用 Bicubic 过滤缩小图像,您将获得最佳结果,但即使如此,使用矢量图形也将优于 Doomsknight 建议的任何基于位图的工作。

Above Accepted cuts the bitmap try my answer以上接受剪切位图试试我的答案

  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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM