在java / android中更改int颜色不透明度 [英] Change int color opacity in java/android

查看:987
本文介绍了在java / android中更改int颜色不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下一个代码更改我的主题颜色的不透明度:

I am trying to change the opacity of a color that I get of my theme with the next code:

TypedValue typedValueDrawerSelected = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValueDrawerSelected, true);
int colorDrawerItemSelected = typedValueDrawerSelected.data;

我希望 colorDrawerItemSelected 保持不变颜色,但它的alpha应该是25%。

I want that the colorDrawerItemSelected keeps the same color, buth its alpha should be 25%.

我发现一些解决方案从imageView获取rgb颜色,但我还没有imageView。

I found some solutions getting the rgb color from the imageView, but I havent imageView.

感谢您的时间。

推荐答案

这还不够吗?

colorDrawerItemSelected = (colorDrawerItemSelected & 0x00FFFFFF) | 0x40000000;

它保存颜色值并将alpha设置为最大值的25%

It saves the color value and sets the alpha to 25% of max.

color int中的第一个字节负责透明度:0 - 完全透明,255(0xFF) - 不透明。在第一部分(&操作)中,我们将第一个字节设置为0并保持其他字节不变。在第二部分中,我们将第一个字节设置为0x40,即0xFF的25%(255 /4≈64)。

First byte in the color int is responsible for the transparency: 0 - completely transparent, 255 (0xFF) – opaque. In the first part ("&" operation) we set the first byte to 0 and left other bytes untouched. In the second part we set the first byte to 0x40 which is the 25% of 0xFF (255 / 4 ≈ 64).

这篇关于在java / android中更改int颜色不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆