[android] #3107 - allow other type of drawable to be used from a resource drawable other than BitmapDrawables
This commit is contained in:
@@ -5,10 +5,12 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.WindowManager;
|
||||
|
||||
@@ -89,7 +91,22 @@ public final class SpriteFactory {
|
||||
}
|
||||
|
||||
public Sprite fromResource(@DrawableRes int resourceId) {
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), resourceId);
|
||||
Drawable drawable = ContextCompat.getDrawable(mContext, resourceId);
|
||||
Bitmap bitmap;
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
|
||||
bitmap = bitmapDrawable.getBitmap();
|
||||
} else {
|
||||
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
|
||||
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
|
||||
} else {
|
||||
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
}
|
||||
return fromBitmap(bitmap);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user