不推荐的替代方案

我有下面的代码,将旋转一个设定量的度可绘制。

    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

74610 次浏览

Do this instead:

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

Kotlin code for BitmapDrawable:

var bitmapDrawable = BitmapDrawable(resources,bitmapObj)

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