Skip to content

Instantly share code, notes, and snippets.

@mluedke2
Created August 31, 2015 17:58
Show Gist options
  • Save mluedke2/ad0b81211a130f03970a to your computer and use it in GitHub Desktop.
Save mluedke2/ad0b81211a130f03970a to your computer and use it in GitHub Desktop.
This is a Utility you can use to customize the UI for a Product Detail Page. Call ColorUtil.getDarkVibrantColor in your PDP and define what to do when color is found.
public class ColorUtil {
private Product currentProduct;
private OnColorFoundListener mOnColorFoundListener;
public void getDarkVibrantColor(Context context, Product product, OnColorFoundListener listener) {
currentProduct = product;
if (currentProduct.color != 0) {
listener.onColorFound(currentProduct.color);
} else {
mOnColorFoundListener = listener;
Picasso.with(context).load(product.image_url).into(target);
}
}
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getDarkVibrantColor(Color.BLACK);
currentProduct.color = bgColor;
mOnColorFoundListener.onColorFound(bgColor);
}
});
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
public interface OnColorFoundListener {
void onColorFound(int color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment