gpt4 book ai didi

RecyclerView 中的 Android 调色板

转载 作者:行者123 更新时间:2023-11-30 02:46:10 26 4
gpt4 key购买 nike

我计划从我的 RecyclerView 中的每个 bitmap 中获取每一种鲜艳的颜色,并将其设置为我的文本的背景。所以我尝试了以下:

        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();
}
}

但是,没有显示 toast,并且我不断收到 空指针异常以下 logcat:

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)

最佳答案

显然,Palette 无法在您的位图中找到鲜艳的颜色。这就是为什么会有 NPE。您可以尝试让调色板生成更多颜色 - Palette p = Palette.generate(bitmap, 24);。但也不能保证它会找到鲜艳的颜色。

遗憾的是,目前还没有官方的Palette 文档。可以引用this article了解更多信息

关于RecyclerView 中的 Android 调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24977856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com