|
@@ -372,6 +372,10 @@ public final class BitmapUtils {
|
|
}
|
|
}
|
|
|
|
|
|
public static Bitmap drawableToBitmap(Drawable drawable) {
|
|
public static Bitmap drawableToBitmap(Drawable drawable) {
|
|
|
|
+ return drawableToBitmap(drawable, -1, -1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Bitmap drawableToBitmap(Drawable drawable, int desiredWidth, int desiredHeight) {
|
|
if (drawable instanceof BitmapDrawable) {
|
|
if (drawable instanceof BitmapDrawable) {
|
|
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
|
|
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
|
|
if (bitmapDrawable.getBitmap() != null) {
|
|
if (bitmapDrawable.getBitmap() != null) {
|
|
@@ -380,20 +384,31 @@ public final class BitmapUtils {
|
|
}
|
|
}
|
|
|
|
|
|
Bitmap bitmap;
|
|
Bitmap bitmap;
|
|
- if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
|
|
|
|
- if (drawable.getBounds().width() > 0 && drawable.getBounds().height() > 0) {
|
|
|
|
- bitmap = Bitmap.createBitmap(drawable.getBounds().width(),
|
|
|
|
- drawable.getBounds().height(),
|
|
|
|
- Bitmap.Config.ARGB_8888);
|
|
|
|
|
|
+ int width;
|
|
|
|
+ int height;
|
|
|
|
+
|
|
|
|
+ if (desiredWidth > 0 && desiredHeight > 0) {
|
|
|
|
+ width = desiredWidth;
|
|
|
|
+ height = desiredHeight;
|
|
|
|
+ } else {
|
|
|
|
+ if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
|
|
|
|
+ if (drawable.getBounds().width() > 0 && drawable.getBounds().height() > 0) {
|
|
|
|
+ width = drawable.getBounds().width();
|
|
|
|
+ height = drawable.getBounds().height();
|
|
|
|
+ } else {
|
|
|
|
+ width = 1;
|
|
|
|
+ height = 1;
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
|
|
|
|
|
|
+ width = drawable.getIntrinsicWidth();
|
|
|
|
+ height = drawable.getIntrinsicHeight();
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
|
|
|
|
- drawable.getIntrinsicHeight(),
|
|
|
|
- Bitmap.Config.ARGB_8888);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ bitmap = Bitmap.createBitmap(width,
|
|
|
|
+ height,
|
|
|
|
+ Bitmap.Config.ARGB_8888);
|
|
|
|
+
|
|
Canvas canvas = new Canvas(bitmap);
|
|
Canvas canvas = new Canvas(bitmap);
|
|
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
drawable.draw(canvas);
|
|
drawable.draw(canvas);
|