[android] #3107 - allow other type of drawable to be used from a resource drawable other than BitmapDrawables

This commit is contained in:
Tobrun
2015-12-18 15:47:54 +01:00
parent f31ff44d79
commit b8aee77299

View File

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