问题描述
我在 res/drawable 中放置了一些位图.
I have placed some bitmaps in res/drawable.
通过 BitmapFactory.decodeResource() 加载位图后,我发现它们会根据密度自动调整大小.
After I load the bitmaps by BitmapFactory.decodeResource(), I find out that they are resized automatically according the density.
这不是我想要的.我希望它们保持原始大小(像素大小).
This is not what I want.I want them to keep their original size (pixel size).
我该怎么办?
推荐答案
正如在另一个问题中所说,使用 drawable-nodpi 文件夹将阻止 android 调整图片大小.
As was said in another question, using the drawable-nodpi folder will prevent android from resizing your pictures.
另外,如果你想拥有多个版本的 hdpi 和 ldpi 格式的图像,但又不希望系统调整它们的大小(例如保持两个分辨率的幂),你可以使用下面的代码,同时加载您的位图:
Additionally, if you want to have multiple versions of an image for hdpi and ldpi format, but don't want the system to resize them (to keep a power of two resolution for example), you can use the following code while loading your bitmpap :
// ask the bitmap factory not to scale the loaded bitmaps
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
// load the bitmap
Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.id.mybmp, opts);
这篇关于我不希望 Android 自动调整我的位图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!