Hopefully fixed OOM when bluring an image.

This commit is contained in:
Karim Abou Zeid 2015-06-29 15:42:33 +02:00
commit bfa800bce6
5 changed files with 34 additions and 19 deletions

View file

@ -27,15 +27,19 @@ package com.kabouzeid.gramophone.helper.bitmapblur;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import com.kabouzeid.gramophone.util.Util;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
// class modified by Karim Abou Zeid (kabouzeid)
public class StackBlurManager { public class StackBlurManager {
static final int EXECUTOR_THREADS = Runtime.getRuntime().availableProcessors(); static final int EXECUTOR_THREADS = Runtime.getRuntime().availableProcessors();
static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(EXECUTOR_THREADS); static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(EXECUTOR_THREADS);
/** /**
* Original image * Resized original image
*/ */
private final Bitmap _image; private final Bitmap _image;
@ -52,10 +56,10 @@ public class StackBlurManager {
/** /**
* Constructor method (basic initialization and construction of the pixel array) * Constructor method (basic initialization and construction of the pixel array)
* *
* @param image The image that will be analyed * @param image The image that will be analysed
*/ */
public StackBlurManager(Bitmap image) { public StackBlurManager(Bitmap image) {
_image = image; _image = Util.getResizedBitmap(image, 500, 500, false);
_blurProcess = new JavaBlurProcess(); _blurProcess = new JavaBlurProcess();
} }
@ -77,13 +81,4 @@ public class StackBlurManager {
public Bitmap returnBlurredImage() { public Bitmap returnBlurredImage() {
return _result; return _result;
} }
/**
* Returns the original image as a bitmap
*
* @return the original bitmap image
*/
public Bitmap getImage() {
return this._image;
}
} }

View file

@ -213,11 +213,12 @@ public class AlbumDetailActivity extends AbsFabActivity implements PaletteColorH
.resetViewBeforeLoading(true) .resetViewBeforeLoading(true)
.build(), .build(),
new SimpleImageLoadingListener() { new SimpleImageLoadingListener() {
@Override @Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
applyPalette(null); applyPalette(null);
albumArtBackground.setImageBitmap(new StackBlurManager(BitmapFactory.decodeResource(getResources(), R.drawable.default_album_art)).process(10)); BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
albumArtBackground.setImageBitmap(new StackBlurManager(BitmapFactory.decodeResource(getResources(), R.drawable.default_album_art, options)).process(10));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
startPostponedEnterTransition(); startPostponedEnterTransition();
} }

View file

@ -68,7 +68,7 @@ import butterknife.InjectView;
/** /**
* A lot of hackery is done in this activity. Changing things may will brake the whole activity. * A lot of hackery is done in this activity. Changing things may will brake the whole activity.
* <p> * <p/>
* Should be kinda stable ONLY AS IT IS!!! * Should be kinda stable ONLY AS IT IS!!!
*/ */
public class ArtistDetailActivity extends AbsFabActivity implements PaletteColorHolder, CabHolder { public class ArtistDetailActivity extends AbsFabActivity implements PaletteColorHolder, CabHolder {
@ -282,8 +282,9 @@ public class ArtistDetailActivity extends AbsFabActivity implements PaletteColor
} }
private void setUpArtistImageAndApplyPalette(final boolean forceDownload) { private void setUpArtistImageAndApplyPalette(final boolean forceDownload) {
final StackBlurManager defaultArtistImageBlurManager = new StackBlurManager(BitmapFactory.decodeResource(getResources(), R.drawable.default_artist_image)); BitmapFactory.Options options = new BitmapFactory.Options();
artistImageBackground.setImageBitmap(defaultArtistImageBlurManager.process(10)); options.inSampleSize = 2;
final StackBlurManager defaultArtistImageBlurManager = new StackBlurManager(BitmapFactory.decodeResource(getResources(), R.drawable.default_artist_image, options));
LastFMArtistImageUrlLoader.loadArtistImageUrl(this, artist.name, forceDownload, new LastFMArtistImageUrlLoader.ArtistImageUrlLoaderCallback() { LastFMArtistImageUrlLoader.loadArtistImageUrl(this, artist.name, forceDownload, new LastFMArtistImageUrlLoader.ArtistImageUrlLoaderCallback() {
@Override @Override
public void onArtistImageUrlLoaded(final String url) { public void onArtistImageUrlLoaded(final String url) {
@ -299,7 +300,7 @@ public class ArtistDetailActivity extends AbsFabActivity implements PaletteColor
@Override @Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
applyPalette(null); applyPalette(null);
artistImageBackground.setImageBitmap(defaultArtistImageBlurManager.returnBlurredImage()); artistImageBackground.setImageBitmap(defaultArtistImageBlurManager.process(10));
} }
@Override @Override

View file

@ -435,7 +435,9 @@ public class MusicControllerActivity extends AbsFabActivity {
applyPalette(null); applyPalette(null);
// to gain some performance cache the blurred bitmap // to gain some performance cache the blurred bitmap
if (defaultAlbumArtStackBlurManager == null) { if (defaultAlbumArtStackBlurManager == null) {
defaultAlbumArtStackBlurManager = new StackBlurManager(BitmapFactory.decodeResource(getResources(), R.drawable.default_album_art)); BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
defaultAlbumArtStackBlurManager = new StackBlurManager(BitmapFactory.decodeResource(getResources(), R.drawable.default_album_art, options));
defaultAlbumArtStackBlurManager.process(10); defaultAlbumArtStackBlurManager.process(10);
} }
albumArtBackground.setImageBitmap(defaultAlbumArtStackBlurManager.returnBlurredImage()); albumArtBackground.setImageBitmap(defaultAlbumArtStackBlurManager.returnBlurredImage());

View file

@ -6,7 +6,9 @@ import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
@ -247,4 +249,18 @@ public class Util {
return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
} else return false; } else return false;
} }
public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth, boolean recycleOld) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
if (recycleOld && resizedBitmap != bm) {
bm.recycle();
}
return resizedBitmap;
}
} }