BitmapDrawable deprecated alternative

若如初见. 提交于 2020-03-08 04:59:00

问题


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


回答1:


Do this instead:

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



回答2:


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



回答3:


Kotlin code for BitmapDrawable:

var bitmapDrawable = BitmapDrawable(resources,bitmapObj)


来源:https://stackoverflow.com/questions/9978884/bitmapdrawable-deprecated-alternative

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!