当前位置: 代码迷 >> 综合 >> new BitmapDrawable(bitmap)已过时 解决方法
  详细解决方案

new BitmapDrawable(bitmap)已过时 解决方法

热度:66   发布时间:2024-01-25 01:37:41.0

使用:

 Drawable drawable = new BitmapDrawable(bitmap);

会提示“已过时,API16: Android4.1之后废弃了此构造函数

解决方法:

Drawable drawable = new BitmapDrawable(getResources(),bitmap);


根据源码介绍,新的构造函数,可以正确设置其目标的密度。

下面附上Android源码↓

/*** Create drawable from a bitmap, not dealing with density.* @deprecated Use {@link #BitmapDrawable(Resources, Bitmap)} to ensure* that the drawable has correctly set its target density.*/@Deprecatedpublic BitmapDrawable(Bitmap bitmap) {this(new BitmapState(bitmap), null);}/*** Create drawable from a bitmap, setting initial target density based on* the display metrics of the resources.*/public BitmapDrawable(Resources res, Bitmap bitmap) {this(new BitmapState(bitmap), res);mBitmapState.mTargetDensity = mTargetDensity;}


  相关解决方案