【问题标题】:Palette Library: How to add transparency to Palette Swatch Color?调色板库:如何为调色板色板颜色添加透明度?
【发布时间】:2016-03-18 07:49:03
【问题描述】:

如何向调色板的色板添加透明度值?就像我将颜色(swatch.getRGB())添加到线性布局一样,它显示纯色。而且我不想使用 alpha,因为它会使布局中的其他项目也变得透明。

我的代码sn-p:

Palette palette = Palette.from(myBitmap).generate();
Palette.Swatch swatch1 = palette.getDarkVibrantSwatch();
int color = swatch1.getRgb();
thatLayout.setBackgroundColor(color)

【问题讨论】:

  • 如果您想在纯色上设置 Alpha,请使用此方法 ColorUtils.setAlphaComponent(int, int)。如果不是,请改写问题。

标签: android android-graphics android-palette


【解决方案1】:

使用android支持实用程序类:

thatLayout.setBackgroundColor(ColorUtils.setAlphaComponent(swatch.getRgb(), alpha));

【讨论】:

    【解决方案2】:

    这就是我得到透明 RGB 整数值的方式

    Bitmap myDisplayBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_pic);
    if (myDisplayBitmap != null && !myDisplayBitmap.isRecycled())
    {
        Palette palette = Palette.from(myDisplayBitmap).generate();
        Palette.Swatch vibrantSwatch = palette.getDarkVibrantSwatch();
    
        /*If vibrantSwatch is null then return 0 otherwise :-) */
        int opaqueDarkVibrantColor = vibrantSwatch != null ? vibrantSwatch.getRgb() : 0;
    
         /*Call the method that returns alpha color */
        int transparentRGBInt = getColorWithAplha(opaqueDarkVibrantColor, 0.5f)
        yourLayout.setBackgroundColor(transparentRGBInt);
    
        // prints something like -2146428888
        Log.i("info", String.valueOf(transparentRGBInt)); 
    
    }
    

    这里是返回alpha值的方法,你需要传递两个参数int RGB颜色值和透明度。

    /**
         * @param color opaque RGB integer color for ex: -11517920
         * @param ratio ratio of transparency for ex: 0.5f
         * @return transparent RGB integer color
         */
        private int getColorWithAplha(int color, float ratio)
        {
            int transColor = 0;
            int alpha = Math.round(Color.alpha(color) * ratio);
            int r = Color.red(color);
            int g = Color.green(color);
            int b = Color.blue(color);
            transColor = Color.argb(alpha, r, g, b);
            return transColor ;
        }
    

    编辑

    使用它从 int RGB 值中获取 Hex 值

    String opHexColor = String.format("#%06X", (0xFFFFFF & opaqueDarkVibrantColor));
    

    【讨论】:

    • 最完美的答案:D
    【解决方案3】:

    我还没有测试过,但是这样的东西应该可以工作

    private int setAlpha(int color, int alpha) {
      alpha = (alpha << 24) & 0xFF000000;
      return alpha | color;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 2018-10-08
      • 2020-02-20
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      相关资源
      最近更新 更多