feat(android): support drawable in ImageSource (#10098)

This allows passing a drawable as another source option
This commit is contained in:
Osei Fortune
2022-11-20 03:13:33 -04:00
committed by GitHub
parent 6c60eab870
commit 75eefa669d
5 changed files with 63 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
@@ -70,6 +72,7 @@ public class Utils {
return bitmap;
}
@SuppressWarnings("deprecation")
public static Bitmap getBitmapFromView(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return drawBitmap(view);
@@ -88,6 +91,28 @@ public class Utils {
}
}
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Rect previousBounds = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
previousBounds = drawable.getBounds();
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
drawable.draw(canvas);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.setBounds(previousBounds);
}
return bitmap;
}
}
public static void drawBoxShadow(View view, String value) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
return;