android:在运行时调整大小的图像质量

发布于 2024-08-17 14:33:05 字数 1550 浏览 6 评论 0原文

我想使用 drawBitmap() 方法显示图像。但在我想根据屏幕尺寸调整它的大小之前。我对结果图像的质量有疑问。您可以在下面看到屏幕截图和测试代码。如何在运行时以良好的质量调整图像大小?

谢谢您的解决方案。

原图: 替代文本 http://dl.dropbox.com/u/709109/gradient.png

结果屏幕截图设备: 替代文本 http://dl.dropbox.com/u/709109/device.png

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import java.io.IOException;
import java.io.InputStream;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout container = (LinearLayout) findViewById(R.id.container);
        container.addView(new View(this) {
            @Override
            protected void onDraw(Canvas canvas) {
                Bitmap unscaledBitmap;
                try {
                    InputStream in = getAssets().open("images/gradient.png");
                    unscaledBitmap = BitmapFactory.decodeStream(in);
                    in.close();
                    canvas.drawBitmap(unscaledBitmap, null, new Rect(0, 0, 320, 480), null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

I want to show images using drawBitmap() method. But before I want to resize it according screen dimensions. I have a problem with quality of result image. You can see screenshots and test code below. How can I resize images in runtime with a good quality?

Thank you for solutions.

Original image:
alt text http://dl.dropbox.com/u/709109/gradient.png

Result screenshot on device:
alt text http://dl.dropbox.com/u/709109/device.png

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import java.io.IOException;
import java.io.InputStream;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout container = (LinearLayout) findViewById(R.id.container);
        container.addView(new View(this) {
            @Override
            protected void onDraw(Canvas canvas) {
                Bitmap unscaledBitmap;
                try {
                    InputStream in = getAssets().open("images/gradient.png");
                    unscaledBitmap = BitmapFactory.decodeStream(in);
                    in.close();
                    canvas.drawBitmap(unscaledBitmap, null, new Rect(0, 0, 320, 480), null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ら栖息 2024-08-24 14:33:05

不要在绘制时调整大小(这将非常昂贵),而是尝试在屏幕外位图中调整大小并确保位图为 32 位 (ARGB888)。

Instead of resizing at drawing time (which is going to be very costly), try to resize in an offscreen bitmap and make sure that Bitmap is 32 bits (ARGB888).

棒棒糖 2024-08-24 14:33:05

请参考Romain的回答下面。

我想删除这个答案,但我无法删除,因为它被标记为已接受。


请注意,Android[最初编写此答案时,没有明确请求 32 位颜色]只会执行 16 位颜色,因此有时您可能会遇到不需要的伪像。

您还可以尝试使用抖动选项来告诉 Android 自动应用抖动,虽然这并不完美,但可以帮助让事情看起来更好一些。

Please refer to Romain's answer below.

I would like to delete this answer, but I am unable to since it's marked as accepted.


Note that Android [when this answer was originally written, without explicitly requesting 32-bit colour] would only do 16-bit colour, so there may be times when you get unwanted artifacts.

You can also try using the dither options to tell Android to automatically apply dithering which, while not perfect, can help make things look a bit better.

如日中天 2024-08-24 14:33:05

查看 http://developer.android.com/reference/android/ Graphics/BitmapFactory.Options.html(与
unscaledBitmap = BitmapFactory.decodeStream(in);

您可以向图像添加质量设置。我猜测质量正在下降,因为您没有传递任何有关如何处理图像的设置。

Check out http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html (To be used with
unscaledBitmap = BitmapFactory.decodeStream(in);

You can add quality settings to the image. I'm guessing that the quality is being degraded as you are not passing any settings for how the image should be processed.

剧终人散尽 2024-08-24 14:33:05

当您尝试缩放没有实际 alpha 值(即每个像素的 alpha = 255)的 ARGB_8888 位图时,旧平台(API 级别 < 12)上的 createScaledBitmap(..) 方法的实现会导致问题,因为它们的 hasAlpha 标志是设置为假。在这种情况下,缩放后的位图会自动转换为 RGB_565 格式。

因此,从 API 级别 12 开始,有一个方法(在 Bitmap 类中)

setHasAlpha(boolean value);

有助于防止使用 createScaledBitmap(..) 时发生转换。

有一种方法可以在 API 级别使用完全相同的方法 > 3.它已经存在很长时间了,只是被隐藏了。此处描述了包括源代码在内的所有必要步骤:

https://stackoverflow.com/a/12202069/1082933

希望如此有帮助。

Implementations of the createScaledBitmap(..) method on older platforms (API level < 12) cause problems when you try to scale an ARGB_8888 Bitmap that has no actual alpha value (i.e. each pixel has alpha = 255), because their hasAlpha flag is set to false. In that case the scaled Bitmap is automatically converted to the RGB_565 format.

Therefore since API level 12 there is a method (in class Bitmap)

setHasAlpha(boolean value);

Which helps to prevent a conversion when using createScaledBitmap(..).

There is a way to use the very same method on API level > 3. It has been present for a very long time, just hidden. All necessary steps including the source code are described here:

https://stackoverflow.com/a/12202069/1082933

Hope this helps.

疏忽 2024-08-24 14:33:05

我遇到了类似的问题,这就是我最终所做的: 质量问题在运行时调整图像大小时

I had a similar problem and here is what I ended up doing: Quality problems when resizing an image at runtime

毁梦 2024-08-24 14:33:05

此外,如果您的调整大小功能不考虑图像的纵横比,质量也会受到影响。您可以在网上找到一些调整大小的算法,例如 看看这个

Also, if your resizing function does not respect the aspect ratio of the image, the quality gets affected. You can find some resizing algorithm on the web, for instance check this out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文