Using palette for each element of a Cursor Adapter

140 views Asked by At

Hello i would like to know how to assign a color for every relativelayout in a Cursor Adapter. I want palette to extract the colors of the album art and use those colors as background to a relative layout.Please help.

public class AlbumAdapter extends CursorAdapter{
private final LayoutInflater mInflater;

public AlbumAdapter(Activity context, Cursor c) {
    super(context, c);
    mInflater= LayoutInflater.from(context);

}


@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        holder.albumTitle = (TextView) view.findViewById(R.id.albumgrid);
        holder.artistName = (TextView) view.findViewById(R.id.artistgrid);
        holder.coverAlbum = (ImageView)view.findViewById(R.id.square_image);
    }view.setTag(holder);


    String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.AlbumColumns.ARTIST));
    String albumname = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.AlbumColumns.ALBUM));

    long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));


    final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
    Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);

    holder.coverAlbum.setBackgroundResource(R.drawable.default_artwork);
    Picasso.with(context).load(albumArtUri)
            .into(holder.coverAlbum);

    holder.albumTitle.setText(albumname);
    holder.artistName.setText(artist);}
static class ViewHolder {
    TextView albumTitle;
    TextView artistName;
    ImageView coverAlbum;

}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);
    return inflater.inflate(R.layout.griditemlayout, parent, false);
}
}




public class AlbumFragment extends Fragment implements LoaderCallbacks<Cursor>{
GridView albumlist;
AlbumAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_album, container, false);
    albumlist = (GridView) myFragmentView.findViewById(R.id.albumList);







    return myFragmentView;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    mAdapter = new AlbumAdapter(getActivity(), null);



    albumlist.setAdapter(mAdapter);

    getLoaderManager().initLoader(0, null, this);



}


@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String select = null;

    return new CursorLoader(getActivity(),MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[] {
            /* 0 */
                    BaseColumns._ID,
            /* 1 */
                    MediaStore.Audio.AlbumColumns.ALBUM,
            /* 2 */
                    MediaStore.Audio.AlbumColumns.ARTIST,
            /* 3 */
                    MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
            /* 4 */
                    MediaStore.Audio.AlbumColumns.FIRST_YEAR,
            /* 5 */
                    MediaStore.Audio.AlbumColumns.ALBUM_KEY
            }, null, null, MediaStore.Audio.AlbumColumns.ALBUM);
}





@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}



@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}
  }
1

There are 1 answers

0
MiguelCatalan On

I know it is a little bit late but, try this:

public final class PaletteGeneratorTransformation
    implements Transformation {

private static final Map<Bitmap, Palette> CACHE = new WeakHashMap<>();
private final int numColors;

@Override
public Bitmap transform(final Bitmap source) {
    if (!CACHE.containsKey(source)) {
        final Palette palette = numColors > 0
                ? Palette.generate(source, numColors)
                : Palette.generate(source);
        CACHE.put(source, palette);
    }

    return source;
}

@Override
public String key() {
    return getClass().getCanonicalName() + ":" + numColors;
}

public PaletteGeneratorTransformation() {
    this(0);
}

public PaletteGeneratorTransformation(final int c) {
    numColors = c;
}

public static abstract class Callback
        implements com.squareup.picasso.Callback {
    private final ImageView target;

    public Callback(final ImageView t) {
        target = t;
    }

    @Override
    public void onSuccess() {
        onPalette(CACHE.get(((BitmapDrawable) target.getDrawable()).getBitmap()));
    }

    @Override
    public void onError() {
        onPalette(null);
    }

    public abstract void onPalette(final Palette palette);
}}

And in your adapter:

Picasso.with(context)
            .load(url)
            .transform(new PaletteGeneratorTransformation(4))
            .into(imageview, new PaletteGeneratorTransformation.Callback(imageview) {
                @Override
                public void onPalette(final Palette palette) {
                    //Tada!
                }
            });

Hope it is useful to someone.