After updating background drawable from host, set a contrast color for text

Let me make this clear: I create a fragment which is used for playing music. I blur the artwork of album and set it as the background of the fragment. And I need to show the music name and artist name on the fragment. Sometimes the updated artwork background has the pretty close color with text (like both them are black). In this case, I want to change the text color to another contrast color to make the text clear. The ios has the similar function when you set the wall paper.

Does anyone know how to achieve that?


Use palette: https://guides.codepath.com/android/Dynamic-Color-using-Palettes

Palette.from(bitmap).maximumColorCount(16).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());
      }
    }
});

This is an example of using it. Mess around with grabbing different swatches to see what works best.


If u need black or white theme for text please go through Determine font color based on background color

or otherwise use palette for vibrent colours https://guides.codepath.com/android/Dynamic-Color-using-Palettes

链接地址: http://www.djcxy.com/p/87044.html

上一篇: 算法从背景创建非常独特,醒目的颜色

下一篇: 从主机更新背景可绘制后,为文本设置对比色