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

使用:

 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.
     */
    @Deprecated
    public 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;
    }


 

猜你喜欢

转载自blog.csdn.net/fengyeNom1/article/details/104001517