Skip to content

Commit

Permalink
Merge pull request #9479 from Stypox/create-scaled-bitmap
Browse files Browse the repository at this point in the history
Use smoother bitmap downscaling for thumbnails
  • Loading branch information
Stypox authored Nov 29, 2022
2 parents 0590350 + b0516fb commit 72a250b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.core.graphics.BitmapCompat;
import androidx.core.math.MathUtils;
import androidx.preference.PreferenceManager;

Expand Down Expand Up @@ -91,8 +92,8 @@ public static void tryResizeAndSetSeekbarPreviewThumbnail(
final float scaleFactor = (float) newWidth / srcWidth;
final int newHeight = (int) (srcBitmap.getHeight() * scaleFactor);

currentSeekbarPreviewThumbnail.setImageBitmap(
Bitmap.createScaledBitmap(srcBitmap, newWidth, newHeight, true));
currentSeekbarPreviewThumbnail.setImageBitmap(BitmapCompat.createScaledBitmap(srcBitmap,
newWidth, newHeight, null, true));
} catch (final Exception ex) {
Log.e(TAG, "Failed to resize and set seekbar preview thumbnail", ex);
currentSeekbarPreviewThumbnail.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.graphics.BitmapCompat;
import androidx.core.graphics.Insets;
import androidx.core.math.MathUtils;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -455,10 +456,11 @@ private void updateEndScreenThumbnail(@Nullable final Bitmap thumbnail) {
}

final float endScreenHeight = calculateMaxEndScreenThumbnailHeight(thumbnail);
final Bitmap endScreenBitmap = Bitmap.createScaledBitmap(
final Bitmap endScreenBitmap = BitmapCompat.createScaledBitmap(
thumbnail,
(int) (thumbnail.getWidth() / (thumbnail.getHeight() / endScreenHeight)),
(int) endScreenHeight,
null,
true);

if (DEBUG) {
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.graphics.BitmapCompat;

import com.squareup.picasso.Cache;
import com.squareup.picasso.LruCache;
Expand Down Expand Up @@ -139,21 +140,23 @@ public Bitmap transform(final Bitmap source) {
.getDimension(R.dimen.player_notification_thumbnail_width),
source.getWidth());

final Bitmap result = Bitmap.createScaledBitmap(
final Bitmap result = BitmapCompat.createScaledBitmap(
source,
(int) notificationThumbnailWidth,
(int) (source.getHeight()
/ (source.getWidth() / notificationThumbnailWidth)),
null,
true);

if (result == source) {
if (result == source || !result.isMutable()) {
// create a new mutable bitmap to prevent strange crashes on some
// devices (see #4638)
final Bitmap copied = Bitmap.createScaledBitmap(
final Bitmap copied = BitmapCompat.createScaledBitmap(
source,
(int) notificationThumbnailWidth - 1,
(int) (source.getHeight() / (source.getWidth()
/ (notificationThumbnailWidth - 1))),
null,
true);
source.recycle();
return copied;
Expand Down

0 comments on commit 72a250b

Please sign in to comment.