Convert defaultArtwork from Bitmap to Drawable

Using bitmaps is still supported, but also this will allow using other types of drawables to be set as artwork.
parent 1f176253
...@@ -25,6 +25,7 @@ import android.graphics.BitmapFactory; ...@@ -25,6 +25,7 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
...@@ -251,7 +252,7 @@ public class PlayerView extends FrameLayout { ...@@ -251,7 +252,7 @@ public class PlayerView extends FrameLayout {
private Player player; private Player player;
private boolean useController; private boolean useController;
private boolean useArtwork; private boolean useArtwork;
private Bitmap defaultArtwork; private Drawable defaultArtwork;
private boolean showBuffering; private boolean showBuffering;
private boolean keepContentOnPlayerReset; private boolean keepContentOnPlayerReset;
private @Nullable ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider; private @Nullable ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider;
...@@ -373,7 +374,7 @@ public class PlayerView extends FrameLayout { ...@@ -373,7 +374,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 = loadFromResource(defaultArtworkId); defaultArtwork = ContextCompat.getDrawable(getContext(), defaultArtworkId);
} }
// Subtitle view. // Subtitle view.
...@@ -421,21 +422,6 @@ public class PlayerView extends FrameLayout { ...@@ -421,21 +422,6 @@ 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}.
* *
...@@ -573,7 +559,7 @@ public class PlayerView extends FrameLayout { ...@@ -573,7 +559,7 @@ public class PlayerView extends FrameLayout {
} }
/** Returns the default artwork to display. */ /** Returns the default artwork to display. */
public Bitmap getDefaultArtwork() { public Drawable getDefaultArtwork() {
return defaultArtwork; return defaultArtwork;
} }
...@@ -584,6 +570,16 @@ public class PlayerView extends FrameLayout { ...@@ -584,6 +570,16 @@ public class PlayerView extends FrameLayout {
* @param defaultArtwork the default artwork to display. * @param defaultArtwork the default artwork to display.
*/ */
public void setDefaultArtwork(Bitmap defaultArtwork) { public void setDefaultArtwork(Bitmap defaultArtwork) {
setDefaultArtwork(new BitmapDrawable(getResources(), defaultArtwork));
}
/**
* Sets the default artwork to display if {@code useArtwork} is {@code true} and no artwork is
* present in the media.
*
* @param defaultArtwork the default artwork to display
*/
public void setDefaultArtwork(Drawable defaultArtwork) {
if (this.defaultArtwork != defaultArtwork) { if (this.defaultArtwork != defaultArtwork) {
this.defaultArtwork = defaultArtwork; this.defaultArtwork = defaultArtwork;
updateForCurrentTrackSelections(/* isNewPlayer= */ false); updateForCurrentTrackSelections(/* isNewPlayer= */ false);
...@@ -1061,7 +1057,7 @@ public class PlayerView extends FrameLayout { ...@@ -1061,7 +1057,7 @@ public class PlayerView extends FrameLayout {
} }
} }
} }
if (setArtworkFromBitmap(defaultArtwork)) { if (setDrawableArtwork(defaultArtwork)) {
return; return;
} }
} }
...@@ -1075,21 +1071,30 @@ public class PlayerView extends FrameLayout { ...@@ -1075,21 +1071,30 @@ public class PlayerView extends FrameLayout {
if (metadataEntry instanceof ApicFrame) { if (metadataEntry instanceof ApicFrame) {
byte[] bitmapData = ((ApicFrame) metadataEntry).pictureData; byte[] bitmapData = ((ApicFrame) metadataEntry).pictureData;
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length); Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
return setArtworkFromBitmap(bitmap); return setArtworkFromBitmap(new BitmapDrawable(getResources(), bitmap));
} }
} }
return false; return false;
} }
private boolean setArtworkFromBitmap(Bitmap bitmap) { private boolean setDrawableArtwork(Drawable drawable) {
if (bitmap != null) { if(drawable instanceof BitmapDrawable) {
int bitmapWidth = bitmap.getWidth(); return setArtworkFromBitmap(((BitmapDrawable) drawable));
int bitmapHeight = bitmap.getHeight(); } else {
artworkView.setImageDrawable(drawable);
}
return true;
}
private boolean setArtworkFromBitmap(BitmapDrawable bitmapDrawable) {
if (bitmapDrawable != null) {
int bitmapWidth = bitmapDrawable.getBitmap().getWidth();
int bitmapHeight = bitmapDrawable.getBitmap().getHeight();
if (bitmapWidth > 0 && bitmapHeight > 0) { if (bitmapWidth > 0 && bitmapHeight > 0) {
if (contentFrame != null) { if (contentFrame != null) {
contentFrame.setAspectRatio((float) bitmapWidth / bitmapHeight); contentFrame.setAspectRatio((float) bitmapWidth / bitmapHeight);
} }
artworkView.setImageBitmap(bitmap); artworkView.setImageBitmap(bitmapDrawable.getBitmap());
artworkView.setVisibility(VISIBLE); artworkView.setVisibility(VISIBLE);
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment