BitmapDrawable已弃用的替代方案

时间:2012-04-02 15:09:47

标签: android drawable

我有以下代码可以将drawable旋转一定的度数。

    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

3 个答案:

答案 0 :(得分:180)

请改为:

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

答案 1 :(得分:0)

BitmapDrawable的Kotlin代码:

          var bitmapDrawable = BitmapDrawable( resources , bitmapObj)

答案 2 :(得分:0)

尝试一次。

  1. 活动中

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

    BitmapDrawable background = new BitmapDrawable(getActivity(), blurImageBg);
    
  3. 在适配器中否则

    BitmapDrawable background = new BitmapDrawable(context.getResources(), blurImageBg);