BitmapDrawable deprecated alternative

AndroidDrawable

Android Problem Overview


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 Solutions


Solution 1 - Android

Do this instead:

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

Solution 2 - Android

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

Solution 3 - Android

Kotlin code for BitmapDrawable:

var bitmapDrawable = BitmapDrawable(resources,bitmapObj)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLee ArmstrongView Question on Stackoverflow
Solution 1 - AndroidonitView Answer on Stackoverflow
Solution 2 - AndroidSurendar DView Answer on Stackoverflow
Solution 3 - AndroidHamed JalilianiView Answer on Stackoverflow