获取绘制在运行时不同的屏幕密度 [英] Get drawable for different screen density at runtime

查看:951
本文介绍了获取绘制在运行时不同的屏幕密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时打印所有可绘制的大小。所以,如果我在华电国际设备,然后我可以打印的华电国际可绘制但如何可以访问,让大小说 MDPI xhdpi 呢?我可以得到以下code访问所有可绘制资源ID:

 最后一类与LT; R.drawable> C = R.drawable.class;
最后一个字段[]栏= c.getDeclaredFields();的for(int i = 0,最大值= fields.length; I<最大;我++){
    最终诠释RESOURCEID;
    尝试{
        RESOURCEID =域[I] .getInt(drawableResources);
    }赶上(例外五){
        继续;
    }
    / *利用RESOURCEID对这里访问可绘制* /
}


解决方案

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

 可绘制可绘制= resources.getDrawableForDensity(ID,DisplayMetrics.DENSITY_XHIGH);

或preferably这个版本的

 可绘制可绘制= resources.getDrawableForDensity(ID,DisplayMetrics.DENSITY_XHIGH,主题);

I want to print sizes of all drawables at run-time. So if I am on hdpi device then I can print the size of hdpi drawables but how to get access to, lets say mdpi and xhdpi as well? I can get access to all drawables resource ids with following code:

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 */
}

解决方案

Ok I found it, Basically you explicitly ask for a particular density drawable like this:

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

or preferably this version

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

这篇关于获取绘制在运行时不同的屏幕密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆