我打算从我的bitmap
中的每个RecyclerView
获取每种鲜艳的颜色,并将其设置为文本的背景。所以我尝试了following
:
Picasso.with(ctx)
.load(curs.getString(curs
.getColumnIndex(TimelineDatabase.KEY_IMAGE)))
.into(holder.ivUserPost, new Callback() {
@Override
public void onSuccess() {
try {
Bitmap bitmap = ((BitmapDrawable) holder.ivUserPost
.getDrawable()).getBitmap();
Palette palette = Palette.generate(bitmap);
Toast.makeText(ctx,
palette.getVibrantColor().getRgb(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
但是,没有显示吐司,并且我一直使用
null pointer exception
logcat接收following
:07-27 07:13:34.163: W/System.err(4900): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.graphics.PaletteItem.getRgb()' on a null object reference
07-27 07:13:34.163: W/System.err(4900): at com.joy.timeline.Fetch$TimelineFetchAdapter$2.onSuccess(Fetch.java:354)
07-27 07:13:34.163: W/System.err(4900): at com.squareup.picasso.RequestCreator.into(RequestCreator.java:514)
07-27 07:13:34.163: W/System.err(4900): at com.joy.timeline.Fetch$TimelineFetchAdapter.onBindViewHolder(Fetch.java:344)
07-27 07:13:34.163: W/System.err(4900): at com.joy.timeline.Fetch$TimelineFetchAdapter.onBindViewHolder(Fetch.java:1)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:2925)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:2527)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:999)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:524)
07-27 07:13:34.163: W/System.err(4900): at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1461)
07-27 07:13:34.164: W/System.err(4900): at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:1600)
07-27 07:13:34.164: W/System.err(4900): at android.view.View.layout(View.java:15273)
07-27 07:13:34.164: W/System.err(4900): at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-27 07:13:34.164: W/System.err(4900): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1069)
最佳答案
显然,调色板在您的位图中找不到鲜艳的色彩。这就是为什么有NPE的原因。您可以尝试让调色板生成更多颜色-Palette p = Palette.generate(bitmap, 24);
。但是也不能保证会找到鲜艳的色彩。
不幸的是,到目前为止还没有官方的Palette
文档。您可以参考this article以获得更多信息。