获取绘制在运行时不同的屏幕密度(Get drawable for different screen

2019-10-23 03:09发布

我想在运行时打印所有可绘制的大小。 所以,如果我在hdpi设备然后我可以打印的尺寸hdpi绘图资源,但如何获得访问,可以说mdpixhdpi呢? 我可以接触到与下面的代码都绘项目资源ID:

final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();

for (int i = 0, max = fields.length; i < max; i++) {
    final int resourceId;
    try {
        resourceId = fields[i].getInt(drawableResources);
    } catch (Exception e) {
        continue;
    }
    /* make use of resourceId for accessing Drawables here */
}

Answer 1:

好吧,我找到了,基本上你明确要求这样一个特定的密度绘制:

Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH);

或者优选该版本

Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH, theme);


文章来源: Get drawable for different screen density at runtime