BitmapDrawable已弃用的替代方案



我有下面的代码,可以将可绘制对象旋转一定的度数。

    public Drawable rotateDrawable(float angle, Context context)
    {
      Bitmap arrowBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.generic2rb);
      // Create blank bitmap of equal size
      Bitmap canvasBitmap = arrowBitmap.copy(Bitmap.Config.ARGB_8888, true);
      canvasBitmap.eraseColor(0x00000000);
      // Create canvas
      Canvas canvas = new Canvas(canvasBitmap);
      // Create rotation matrix
      Matrix rotateMatrix = new Matrix();
      rotateMatrix.setRotate(angle, canvas.getWidth()/2, canvas.getHeight()/2);
      //Draw bitmap onto canvas using matrix
      canvas.drawBitmap(arrowBitmap, rotateMatrix, null);
      return new BitmapDrawable(canvasBitmap); 
    }

新的SDK说

BitmapDrawable(canvasBitmap); 

已弃用。有其他选择吗?

http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html

改为执行以下操作:

return new BitmapDrawable(context.getResources(), canvasBitmap);

试试这个。

  1. 活动中

    BitmapDrawable background = new BitmapDrawable(this.getResources(), blurImageBg);
    
  2. 片段内

    BitmapDrawable background = new BitmapDrawable(getActivity(), blurImageBg);
    
  3. 在适配器或其他中

    BitmapDrawable background = new BitmapDrawable(context.getResources(), blurImageBg);
    
BitmapDrawable:的Kotlin代码
var bitmapDrawable = BitmapDrawable(resources,bitmapObj)

最新更新