Skip to content

Commit

Permalink
Move palette/color extraction to AsyncTask and made colors semi-trans…
Browse files Browse the repository at this point in the history
…parent when not found.
  • Loading branch information
tony-w committed Feb 25, 2015
1 parent 7557086 commit 0a19af4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.tonyw.sampleapps.palettecolorextraction;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Parcelable;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.github.johnpersano.supertoasts.SuperActivityToast;
import com.github.johnpersano.supertoasts.SuperToast;
Expand Down Expand Up @@ -101,64 +95,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
((ImageView) cardView.findViewById(R.id.card_image)).setImageBitmap(bitmap);

// Extract prominent colors asynchronously and then update the card.
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
View vibrantView = cardView.findViewById(R.id.vibrant);
int vibrantColor = palette.getVibrantColor(Color.BLACK);
getGradientDrawable(vibrantView).setColor(vibrantColor);
vibrantView.setTag(vibrantColor); // For retrieval when long-clicking.
vibrantView.setOnLongClickListener(mLongClickListener);

View vibrantDarkView = cardView.findViewById(R.id.vibrant_dark);
int vibrantDarkColor = palette.getDarkVibrantColor(Color.BLACK);
getGradientDrawable(vibrantDarkView).setColor(vibrantDarkColor);
vibrantDarkView.setTag(vibrantDarkColor); // For retrieval when long-clicking.
vibrantDarkView.setOnLongClickListener(mLongClickListener);

View vibrantLightView = cardView.findViewById(R.id.vibrant_light);
int vibrantLightColor = palette.getLightVibrantColor(Color.BLACK);
getGradientDrawable(vibrantLightView).setColor(vibrantLightColor);
vibrantLightView.setTag(vibrantLightColor); // For retrieval when long-clicking.
vibrantLightView.setOnLongClickListener(mLongClickListener);

View mutedView = cardView.findViewById(R.id.muted);
int mutedColor = palette.getMutedColor(Color.BLACK);
getGradientDrawable(mutedView).setColor(mutedColor);
mutedView.setTag(mutedColor); // For retrieval when long-clicking.
mutedView.setOnLongClickListener(mLongClickListener);

View mutedDarkView = cardView.findViewById(R.id.muted_dark);
int mutedDarkColor = palette.getDarkMutedColor(Color.BLACK);
getGradientDrawable(mutedDarkView).setColor(mutedDarkColor);
mutedDarkView.setTag(mutedDarkColor); // For retrieval when long-clicking.
mutedDarkView.setOnLongClickListener(mLongClickListener);

View mutedLightView = cardView.findViewById(R.id.muted_light);
int mutedLightColor = palette.getLightMutedColor(Color.BLACK);
getGradientDrawable(mutedLightView).setColor(mutedLightColor);
mutedLightView.setTag(mutedLightColor); // For retrieval when long-clicking.
mutedLightView.setOnLongClickListener(mLongClickListener);
}
});
new ExtractPaletteColorsAsyncTask(mContext, cardView).execute(bitmap);

return cardView;
}

private GradientDrawable getGradientDrawable(View colorShape) {
return (GradientDrawable) colorShape.getBackground();
}

private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String colorHex = String.format("%06X", (0xFFFFFF & (int) v.getTag()));
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Color Hex", colorHex);
clipboard.setPrimaryClip(clip);
Toast.makeText(mContext, "Copied color '" + colorHex + "' to clipboard.",
Toast.LENGTH_SHORT).show();
return true;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.tonyw.sampleapps.palettecolorextraction;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.AsyncTask;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.widget.Toast;

/**
* An AsyncTask dedicated to extracting prominent colors from bitmaps and updating them on a card.
*/
public class ExtractPaletteColorsAsyncTask extends AsyncTask<Bitmap, Void, Palette> {
private Context mContext;
private View mCardView;

public ExtractPaletteColorsAsyncTask(Context context, View cardView) {
mContext = context;
mCardView = cardView;
}

@Override
protected Palette doInBackground(Bitmap... bitmaps) {
return Palette.generate(bitmaps[0]);
}

@Override
protected void onPostExecute(Palette palette) {
View vibrantView = mCardView.findViewById(R.id.vibrant);
int vibrantColor = palette.getVibrantColor(Color.WHITE);
getGradientDrawable(vibrantView).setColor(vibrantColor);
boolean colorFound = palette.getVibrantSwatch() != null;
vibrantView.setTag(colorFound ? vibrantColor : null); // For retrieval when long-clicking.
vibrantView.setAlpha(colorFound ? 1.0f : 0.26f);
vibrantView.setOnLongClickListener(mLongClickListener);

View vibrantDarkView = mCardView.findViewById(R.id.vibrant_dark);
int vibrantDarkColor = palette.getDarkVibrantColor(Color.WHITE);
getGradientDrawable(vibrantDarkView).setColor(vibrantDarkColor);
colorFound = palette.getDarkVibrantSwatch() != null;
vibrantDarkView.setTag(colorFound ? vibrantDarkColor : null);
vibrantDarkView.setAlpha(colorFound ? 1.0f : 0.26f);
vibrantDarkView.setOnLongClickListener(mLongClickListener);

View vibrantLightView = mCardView.findViewById(R.id.vibrant_light);
int vibrantLightColor = palette.getLightVibrantColor(Color.WHITE);
getGradientDrawable(vibrantLightView).setColor(vibrantLightColor);
colorFound = palette.getLightVibrantSwatch() != null;
vibrantLightView.setTag(colorFound ? vibrantLightColor : null);
vibrantLightView.setAlpha(colorFound ? 1.0f : 0.26f);
vibrantLightView.setOnLongClickListener(mLongClickListener);

View mutedView = mCardView.findViewById(R.id.muted);
int mutedColor = palette.getMutedColor(Color.WHITE);
getGradientDrawable(mutedView).setColor(mutedColor);
colorFound = palette.getMutedSwatch() != null;
mutedView.setTag(colorFound ? mutedColor : null);
mutedView.setAlpha(colorFound ? 1.0f : 0.26f);
mutedView.setOnLongClickListener(mLongClickListener);

View mutedDarkView = mCardView.findViewById(R.id.muted_dark);
int mutedDarkColor = palette.getDarkMutedColor(Color.WHITE);
getGradientDrawable(mutedDarkView).setColor(mutedDarkColor);
colorFound = palette.getDarkMutedSwatch() != null;
mutedDarkView.setTag(colorFound ? mutedDarkColor : null);
mutedDarkView.setAlpha(colorFound ? 1.0f : 0.26f);
mutedDarkView.setOnLongClickListener(mLongClickListener);

View mutedLightView = mCardView.findViewById(R.id.muted_light);
int mutedLightColor = palette.getLightMutedColor(Color.WHITE);
getGradientDrawable(mutedLightView).setColor(mutedLightColor);
colorFound = palette.getLightMutedSwatch() != null;
mutedLightView.setTag(colorFound ? mutedLightColor : null);
mutedLightView.setAlpha(colorFound ? 1.0f : 0.26f);
mutedLightView.setOnLongClickListener(mLongClickListener);
}

private GradientDrawable getGradientDrawable(View colorShape) {
return (GradientDrawable) colorShape.getBackground();
}

private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (v.getTag() == null) {
Toast.makeText(mContext, "No color available.", Toast.LENGTH_SHORT).show();
} else {
String colorHex = String.format("%06X", (0xFFFFFF & (int) v.getTag()));
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Color Hex", colorHex);
clipboard.setPrimaryClip(clip);
Toast.makeText(mContext, "Copied color '" + colorHex + "' to clipboard.",
Toast.LENGTH_SHORT).show();
}
return true;
}
};
}

0 comments on commit 0a19af4

Please sign in to comment.