Is it possible to access a drawable from a specific density in Android?

廉价感情. 提交于 2019-12-24 03:07:46

问题


Depending on the situation of course, I normally have a drawable for each density my app supports and the right density drawable is used when I use an id reference of some drawable.

But I'm looking to access a drawable from a specific density (for example, ldpi) no matter the density running on the device. But I want this on a specific situation, not everywhere, that would defeat the purpose of having multiple drawables for each density.

Is this possible?


回答1:


I dont know that it is possible or not but as a simple logic.

You just make a copy of that ldpi drawable and name it mypic2 and keep it in ldpi folder.

now from your xml or activity give the reference of that new mypic2 at your specific situation.




回答2:


Here is what I used to load a hdpi drawable from another app (works only with Android 4.0)

PackageInfo pi = packages.get(i);

ai = new AppListAdapter.AppItem();
try {
    Context otherAppCtx = getApplicationContext().createPackageContext(pi.applicationInfo.packageName, CONTEXT_IGNORE_SECURITY);

    ai.Icon = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon,  DisplayMetrics.DENSITY_HIGH);

} catch (Exception e) {
    ai.Icon = pi.applicationInfo.loadIcon(getPackageManager());

}



回答3:


If it's one specific drawable, then just have it in the ldpi folder. The runtime will pick it up from there if it can't find it in any of the more appropriate density folders.



来源:https://stackoverflow.com/questions/7793031/is-it-possible-to-access-a-drawable-from-a-specific-density-in-android

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