获取绘制的密度API级别< 15 [英] Get drawable for density for API Level <15

查看:293
本文介绍了获取绘制的密度API级别< 15的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Android的API 15级就有<一个href="http://developer.android.com/reference/android/content/res/Resources.html#getDrawableForDensity%28int,%20int%29">method 市民可绘制getDrawableForDensity(INT ID,INT密度)来检索特定的屏幕密度的绘制对象。有没有办法做到这一点之前,API等级15?

Since Android API Level 15 there is the method public Drawable getDrawableForDensity (int id, int density) to retrieve a drawable object for a specific screen density. Is there any way to do this prior to API Level 15?

推荐答案

所以,其实我觉得它可能采用Android的API逆向工程和源 - code按@ adtennant的建议得到解决。我开始写的解决方案,但在这一过程中进入了死胡同作为底层的本机(非Java)API,它需要的是更高版本的Andr​​oid无法使用。

So I actually thought it could be resolved using reverse-engineering of Android APIs and the source-code as per @adtennant's suggestion. I started writing the solution it, but in the process hit a dead end as an underlying native (non-Java) API that is necessary is unavailable in later versions of Android.

如果你是没事制约这只是位图,所暗示您的评论,就可以与BitmapFactory做到这一点的建议。 BitmapFactory有另一种方法:

If you are okay constraining this to just Bitmaps, as implied by your comment, it is possible to do this with BitmapFactory as suggested. BitmapFactory has another method:

<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cResource%28android.content.res.Resources,%20int,%20android.graphics.BitmapFactory.Options%29"相对=nofollow>德codeResource(资源RES,INT ID,BitmapFactory.Options选择采用)

此方法接受额外的<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inTargetDensity"相对=nofollow> BitmapFactory.Options ,这将允许你指定装载密度。这似乎是因为API级别1。具体到已经存在的,我相信你可以使用:

This method accepts the additional BitmapFactory.Options which will allow you to specify the density to load. This appears to have existed since API level 1. Specifically, I believe that you can use:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inDensity = DisplayMetrics.DENSITY_MEDIUM; // whichever you want to load
options.inTargetDensity = getResources().getDisplayMetrics().densityDpi;
options.inScaled = true;

这也将它扩展到了屏幕像素密度,如果加载了不匹配的密度。

This will also scale it to the screen density if a mismatched density is loaded.

这篇关于获取绘制的密度API级别&LT; 15的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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