BitmapDrawable deprecated alternative

83

I have the following code that will rotate a drawable by a set amount of degrees.

    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); 
    }

The new SDK says that the

BitmapDrawable(canvasBitmap); 

is deprecated. Any alternatives at all?

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

android
drawable
asked on Stack Overflow Apr 2, 2012 by Lee Armstrong • edited Apr 2, 2012 by Lee Armstrong

3 Answers

188

Do this instead:

return new BitmapDrawable(context.getResources(), canvasBitmap);
answered on Stack Overflow Apr 2, 2012 by onit • edited May 27, 2017 by Teovald
0

Kotlin code for BitmapDrawable:

var bitmapDrawable = BitmapDrawable(resources,bitmapObj)
answered on Stack Overflow Feb 17, 2019 by Hamed Jaliliani • edited Aug 17, 2019 by evandrix
0

Try this Once.

  1. In Activity

    BitmapDrawable background = new BitmapDrawable(this.getResources(), blurImageBg);
    
  2. In Fragment

    BitmapDrawable background = new BitmapDrawable(getActivity(), blurImageBg);
    
  3. In Adapter or else

    BitmapDrawable background = new BitmapDrawable(context.getResources(), blurImageBg);
    
answered on Stack Overflow Aug 30, 2019 by Surendar D • edited Aug 30, 2019 by Suraj Rao

User contributions licensed under CC BY-SA 3.0