'getDrawable(int)' is deprecated,getDrawable过时

来源:互联网 发布:女狙击手知乎 编辑:程序博客网 时间:2024/05/18 02:24

getDrawable(int)在API 21(5.0)已经过时了



5.0之后使用:

ContextCompat.getDrawable(context, R.drawable.your_drawable)

例如:

imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon));


源码

 public static final Drawable getDrawable(Context context, @DrawableRes int id) {        final int version = Build.VERSION.SDK_INT;        if (version >= 21) {            return ContextCompatApi21.getDrawable(context, id);        } else if (version >= 16) {            return context.getResources().getDrawable(id);        } else {            // Prior to JELLY_BEAN, Resources.getDrawable() would not correctly            // retrieve the final configuration density when the resource ID            // is a reference another Drawable resource. As a workaround, try            // to resolve the drawable reference manually.            final int resolvedId;            synchronized (sLock) {                if (sTempValue == null) {                    sTempValue = new TypedValue();                }                context.getResources().getValue(id, sTempValue, true);                resolvedId = sTempValue.resourceId;            }            return context.getResources().getDrawable(resolvedId);        }    }
可以看出也是做了高版本低版本兼容的


原创粉丝点击