Image loaded from assets folder comes in different size than res/drawable

孤人 提交于 2019-12-06 06:09:42

When you put a bitmap in a drawable-<density> folder, and there is no variant for the device's exact display density, the framework will auto-scale it considering the ratio between <density> and the device's density.

This is done so that the image dimensions, in dps, remains constant between devices (and that you are not obligated from providing a variant for each possible density).

When you load from assets, the "source" density is unknown, so this autoscaling is not performed. Hence, the difference.

If you want to load the image from the assets "as if it were hdpi", you can do something like:

Options opts = new BitmapFactory.Options();
opts.inDensity = DisplayMetrics.DENSITY_HIGH;
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, srcName, opts);

That said, I don't see any problems at all with including all the files in drawable folders.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!