这个问题困扰我好久了。
谷歌搜索后没有找到太多有用的信息,我来这里寻求帮助。
假设我有一个图像(分辨率 100px*100px),然后将它设置为一个 ImageView(w*h 100dp*100dp)。
如果设备类型是hdpi,我把图片放在drawable,drawable-hdpi,drawable-mdpi,drawable-xhdpi,drawable-nohpi文件夹下需要多少内存?
是否有一个规则可以让我们得到结果?
多谢你们。
最佳答案
Drawable类是所有图形类的基类。这是您在 Canvas Canvas 上的主题的一般抽象。因此您可以从中创建位图。
首先,它取决于设置为位图的配置。你可以计算它:
/**
* A helper function to return the byte usage per pixel of a bitmap based on its configuration.
*/
static int getBytesPerPixel(Config config) {
if (config == Config.ARGB_8888) {
return 4;
} else if (config == Config.RGB_565) {
return 2;
} else if (config == Config.ARGB_4444) {
return 2;
} else if (config == Config.ALPHA_8) {
return 1;
}
return 1;
}
因此您可以计算将分配给该对象的字节数:
getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
int count = pixels.length;
long allBytes = getBytesPerPixel(config)*count; // your memory allocation
中学。您需要了解 hdpi、xhdpi、mdpi 等。概念。
假设您有一个小屏幕。 320x240 像素
通常,具有此屏幕分辨率的设备内存也很低。所以您需要了解 - 小屏幕上的大图片是 OutOfMemoryError 的正确方法。
关于android - 在不同的 drawable 文件夹中位图需要多少内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655386/