Android如何从特定图像设置动态背景颜色

5

我看过一些像 DataDex 这样的应用程序,它们可能拥有类似动态颜色的卡片视图和背景,如下图所示:

enter image description here

这种颜色也适用于详细视图:

enter image description here

我猜这些颜色是从图片本身生成的,但我不知道使用的是哪个库或API。有任何提示吗?


你可以使用调色板。 - Shalu T D
您需要使用调色板 API:https://developer.android.com/training/material/palette-colors - Chintan Soni
1个回答

2

您可以使用调色板来使用动态颜色。 Material Design鼓励在处理丰富图像时动态使用颜色。新的调色板支持库可让您从图像中提取一小组颜色,以使UI控件与之匹配,从而创建沉浸式体验。

您可以通过两种方式实现调色板:

同步方式:

Palette palette = Palette.from(bitmap).generate();
// OR
Palette palette = Palette.from(bitmap).maximumColorCount(numberOfColors).generate();

传入的位图是您要提取颜色的图像。默认情况下,它将使用最大调色板大小为16种颜色。然后我们可以使用如下所示的颜色。
// Pick one of the swatches
Palette.Swatch vibrant = palette.getVibrantSwatch();
if (vibrant != null) {
    // Set the background color of a layout based on the vibrant color
    containerView.setBackgroundColor(vibrant.getRgb());
    // Update the title TextView with the proper text color
    titleView.setTextColor(vibrant.getTitleTextColor());
}

异步地:

通过将 PaletteAsyncListener 传递给生成方法,现在将使用 AsyncTask 异步地生成调色板来从位图中收集调色板样本信息:

// This is the quick and easy integration path. 
// May not be optimal (since you're dipping in and out of threads)
Palette.from(bitmap).maximumColorCount(numberOfColors).generate(new Palette.PaletteAsyncListener() {
    @Override
    public void onGenerated(Palette palette) {
         // Get the "vibrant" color swatch based on the bitmap
         Palette.Swatch vibrant = palette.getVibrantSwatch();
          if (vibrant != null) {
              // Set the background color of a layout based on the vibrant color
              containerView.setBackgroundColor(vibrant.getRgb());
              // Update the title TextView with the proper text color
              titleView.setTextColor(vibrant.getTitleTextColor());
          }
    }
});

更多信息,请查看文档


你能帮我检查一下关于流程图的另一个问题吗?链接在这里:https://stackoverflow.com/questions/62660155/android-how-to-make-flow-charts?noredirect=1#comment110810281_62660155 - Nexussim Lements

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接