你好,我想知道如何为游标适配器中的每个相对布局分配颜色。我想要调色板提取相册艺术的颜色,并使用这些颜色作为相对布局的背景。请帮忙。
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);
}
}
我知道有点晚了,但是,试试这个:
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);
}}
在您的适配器中:
Picasso.with(context)
.load(url)
.transform(new PaletteGeneratorTransformation(4))
.into(imageview, new PaletteGeneratorTransformation.Callback(imageview) {
@Override
public void onPalette(final Palette palette) {
//Tada!
}
});
希望它对某人有用。