How to update an already created Drawable with Bitmap without creating a new Drawable? Ie, approximately the case is approximately as follows:

// сначала создается и используется Drawable Drawable drawable = new MyDrawable(context.getResources(), bitmap); // Затем прилетает новый Bitmap, который необходимо загрузить в Drawable makeSomeAsyncOperation((newBitmap) -> drawable.setBitmap(newBitmap)); 

MyDrawable looks like this:

 class MyDrawable extends BitmapDrawable { public void setBitmap(@NonNull Bitmap bitmap) { // ??? } } 

A bit specific implementation, but the other way is not suitable here.

    1 answer 1

    The BitmapDrawable class has a hidden setBitmap method. You can try to call it using reflection:

     public class MyDrawable extends BitmapDrawable { public void updateBitmap(@NonNull Bitmap bitmap) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { Class c = getClass().getSuperclass(); Method m = c.getDeclaredMethod("setBitmap", Bitmap.class); if (!m.isAccessible()) { m.setAccessible(true); } m.invoke(this, bitmap); } } 

    Since this method is hidden, then the SDK developers would not want someone to use it. So this code may stop working in any next version. I do not recommend using this method, just described what is possible.