Skip to content

Instantly share code, notes, and snippets.

@almozavr
Forked from imminent/ExampleActivity.java
Last active August 29, 2015 14:19
Show Gist options
  • Save almozavr/501d5b3643f0be233525 to your computer and use it in GitHub Desktop.
Save almozavr/501d5b3643f0be233525 to your computer and use it in GitHub Desktop.
Gist for a modified approach to integrating Palette with Picasso proposed by Jake Wharton for the interim while Picasso doesn't have a supported way to pass meta data along the pipeline. http://jakewharton.com/coercing-picasso-to-play-with-palette/ Fork provides diff RoundedImageView support
package your.package;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.widget.ImageView;
public abstract class AndroidPaletteCallback extends PaletteCallback<ImageView> {
public AndroidPaletteCallback(@NonNull ImageView imageView) {
super(imageView);
}
@Override protected Bitmap getBitmap() {
return ((BitmapDrawable) getImageView().getDrawable()).getBitmap();
}
}
package your.package;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import com.squareup.picasso.Picasso;
import your.package.PaletteTransformation;
import static your.package.PaletteTransformation.PaletteCallback;
public class ExampleActivity extends Activity {
@Override public void onCreate(Bundle icicle) {
super.onCreate();
// Apply to ImageView
Picasso.with(context)
.load(url)
.fit().centerCrop()
.transform(PaletteTransformation.instance())
.into(imageView, new AndroidPaletteCallback(imageView) {
@Override public void onSuccess(Palette palette) {
// TODO apply palette to text views, backgrounds, etc.
}
});
// Apply to RoundedImageView
Picasso.with(context)
.load(url)
.fit().centerCrop()
.transform(PaletteTransformation.instance())
.into(roundedImageView, new RoundedPaletteCallback(roundedImageView) {
@Override public void onSuccess(Palette palette) {
// TODO apply palette to text views, backgrounds, etc.
}
});
}
}
package your.package;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;
import com.squareup.picasso.Callback;
import java.lang.ref.WeakReference;
/**
* A {@link Callback} that receives {@link Palette} information in its callback.
*
* @see Callback
*/
public abstract class PaletteCallback<T extends ImageView> implements Callback {
private WeakReference<T> mImageView;
public PaletteCallback(@NonNull T imageView) {
mImageView = new WeakReference<>(imageView);
}
protected abstract void onSuccess(Palette palette);
@Override public final void onSuccess() {
if (getImageView() == null) {
return;
}
final Bitmap bitmap = getBitmap(); // Ew!
final Palette palette = PaletteTransformation.getPalette(bitmap);
onSuccess(palette);
}
@Override public void onError() {
if (onError != null) {
onError.call();
}
}
protected abstract Bitmap getBitmap();
protected T getImageView() {
return mImageView.get();
}
}
package your.package;
import android.graphics.Bitmap;
import android.support.v7.graphics.Palette;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Transformation used to extract {@link Palette} information from the {@linkplain Bitmap}.
*/
public final class PaletteTransformation implements Transformation {
private static final PaletteTransformation INSTANCE = new PaletteTransformation();
private static final Map<Bitmap, Palette> CACHE = new WeakHashMap<Bitmap, Palette>();
/**
* A {@link Target} that receives {@link Palette} information in its callback.
*
* @see Target
*/
public static abstract class PaletteTarget implements Target {
/**
* Callback when an image has been successfully loaded.
* Note: You must not recycle the bitmap.
*
* @param palette The extracted {@linkplain Palette}
*/
protected abstract void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from, Palette palette);
@Override public final void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
final Palette palette = getPalette(bitmap);
onBitmapLoaded(bitmap, from, palette);
}
}
public static Palette getPalette(Bitmap bitmap) {
return CACHE.get(bitmap);
}
/**
* Obtains a {@link PaletteTransformation} to extract {@link Palette} information.
*
* @return A {@link PaletteTransformation}
*/
public static PaletteTransformation instance() {
return INSTANCE;
}
//# Transformation Contract
@Override public final Bitmap transform(Bitmap source) {
final Palette palette = Palette.generate(source);
CACHE.put(source, palette);
return source;
}
@Override public String key() {
return ""; // Stable key for all requests. An unfortunate requirement.
}
private PaletteTransformation() { }
}
package your.package;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import com.makeramen.roundedimageview.RoundedDrawable;
import com.makeramen.roundedimageview.RoundedImageView;
public abstract class RoundedPaletteCallback extends PaletteCallback<RoundedImageView> {
public RoundedPaletteCallback(@NonNull RoundedImageView imageView) {
super(imageView);
}
@Override protected Bitmap getBitmap() {
return ((RoundedDrawable) getImageView().getDrawable()).getSourceBitmap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment