Support vector drawables to be used as default artwork

This commit is contained in:
Jovche Mitrejchevski 2018-07-24 08:16:53 +02:00
parent 61b838f414
commit 1f1762538f

View File

@ -22,10 +22,15 @@ import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -368,7 +373,7 @@ public class PlayerView extends FrameLayout {
artworkView = findViewById(R.id.exo_artwork); artworkView = findViewById(R.id.exo_artwork);
this.useArtwork = useArtwork && artworkView != null; this.useArtwork = useArtwork && artworkView != null;
if (defaultArtworkId != 0) { if (defaultArtworkId != 0) {
defaultArtwork = BitmapFactory.decodeResource(context.getResources(), defaultArtworkId); defaultArtwork = loadFromResource(defaultArtworkId);
} }
// Subtitle view. // Subtitle view.
@ -416,6 +421,21 @@ public class PlayerView extends FrameLayout {
hideController(); hideController();
} }
private Bitmap loadFromResource(int defaultArtworkId) {
Drawable drawable = ContextCompat.getDrawable(getContext(), defaultArtworkId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap 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 bitmap;
}
/** /**
* Switches the view targeted by a given {@link Player}. * Switches the view targeted by a given {@link Player}.
* *