为什么 BitmapFactory.decodeResource 缩放我的图像? [英] Why does BitmapFactory.decodeResource scale my image?

查看:45
本文介绍了为什么 BitmapFactory.decodeResource 缩放我的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 /res/drawable/ 目录中有一个 120x120px 的图像.这是所有设备所需的大小.

I have an image that is 120x120px located in my /res/drawable/ directory. This is the size required for all devices.

要加载此位图,我使用以下内容:

To load this Bitmap, I am using the following:

Bitmap tmpBmp = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);

问题是当我测量 tmpBmp 时,尺寸是 360x360px.我知道如何将其缩小到我需要的 120x120,但这浪费了处理时间,尽管很少.

The issue is that when I measure tmpBmp, the size is 360x360px. I know how to scale this back down to the 120x120 that I need, but it is a waste of processing time, although minimal.

我假设这与屏幕密度有关,因为我的资源位于无密度目录中.

I am assuming that this has something to do with screen densities, since my resource is in a density-less directory.

decodeResource 缩放我的图像的原因是什么?我如何阻止它这样做?

What is the reason for decodeResource scaling my image? And how do I stop it from doing so?

推荐答案

默认的 drawable 目录假定图像需要从默认的 mdpi 大小开始缩放.如果您想禁用资源缩放,请将您的图像放在 drawable-nodpi 中.

The default drawable directory assumes that images need to be scaled from the default mdpi size. Put your images in drawable-nodpi if you want to disable resource scaling.

请注意,如果显示在屏幕上,则 120x120 像素的图像在 xxhdpi 设备上将比 mdpi 设备小 3 倍(因为每英寸的像素数是其三倍).

Do note that a 120x120px image, if displayed on the screen, will be 3x smaller on a xxhdpi device compared to a mdpi device (as there are three times as many pixels per inch).

这篇关于为什么 BitmapFactory.decodeResource 缩放我的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆