1

我想在运行时打印所有可绘制对象的大小。因此,如果我在hdpi设备上,那么我可以打印hdpi可绘制对象的大小,但是如何访问,mdpi比如说xhdpi?我可以使用以下代码访问所有可绘制资源 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 */
}
4

1 回答 1

3

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

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

或者最好是这个版本

Drawable drawable = resources.getDrawableForDensity(id, DisplayMetrics.DENSITY_XHIGH, theme);
于 2015-05-15T08:18:59.860 回答