Android Studio问题解决系列—Resources中getDrawable(int)过时

来源:互联网 发布:知福茶叶公司 编辑:程序博客网 时间:2024/05/16 23:51

今天在Android Studio 1.2.2中编译代码时遇到Resources中getDrawable(int)已过时的警告信息:

    Warning:(133, 52) java: android.content.res.Resources中的getDrawable(int)已过时


    查看[1]可总结出以下信息:

Resources类中有两个方法在API level 22中被废弃:

    废弃接口:Drawable getDrawable(int id)

    替代接口:Drawable getDrawable(int id, Resources.Theme theme), 第二个参数@theme可以为空值.

         或Context.getDrawable(int)

    废弃原因:在JELLY_BEAN(也就是Android 4.1版本)之前, 当这里传递的资源ID是另一个Drawable资源的别名, 则该函数不能正确地获取到最终配置density。即别名资源(alias resource)的density配置不同于实际资源的, 返回的Drawable对象的density将不正确, 这样缩放时将出错。

 

    废弃接口:Drawable getDrawableForDensity(int id, int density)

    替代接口:Drawable getDrawableForDensity(int id, int density, Resources.Theme theme)


检查getDrawable(int id)函数实现,当被调用时将输出下面警告信息并给出修改方案

参考资料:

[1] Class Resources, http://developer.android.com/intl/zh-cn/reference/android/content/res/Resources.html

0 0