繁体   English   中英

调色板库:如何为Palette Swatch Color添加透明度?

[英]Palette Library: How to add transparency to Palette Swatch Color?

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

我的代码片段:

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

使用android支持实用程序类:

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

这就是我得到透明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值的方法,你需要传递两个params 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));

我没有测试过这个,但是这样的东西应该可行

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM