本文翻译自:How to make a background 20% transparent on Android

我如何使Textview的背景大约20%透明(不完全透明),背景中是否有颜色(即白色)?


#1楼

参考:https://stackoom.com/question/lLzd/如何在Android上使背景-透明


#2楼

Use a color with an alpha value like #33------ , and set it as background of your editText using the XML attribute android:background=" " . 使用具有#33------这样的alpha值的颜色,并使用XML属性android:background=" "将其设置为editText的android:background=" "

  1. 0% (transparent) -> #00 in hex 0%(透明)->#00(十六进制)
  2. 20% -> #33 20%->#33
  3. 50% -> #80 50%->#80
  4. 75% -> #C0 75%->#C0
  5. 100% (opaque) -> #FF 100%(不透明)-> #FF

255 * 0.2 = 51 → in hex 33 255 * 0.2 = 51→十六进制33


#3楼

Make the color have 80% in the alpha channel. 使颜色在Alpha通道中占80%。 For example, for red use #CCFF0000 : 例如,对于红色,请使用#CCFF0000

<TextView...android:background="#CCFF0000" />

In the example, CC is the hexadecimal number for 255 * 0.8 = 204 . 在示例中, CC255 * 0.8 = 204的十六进制数。 Note that the first two hexadecimal digits are for the alpha channel. 请注意,前两个十六进制数字用于alpha通道。 The format is #AARRGGBB , where AA is the alpha channel, RR is the red channel, GG is the green channel and BB is the blue channel. 格式为#AARRGGBB ,其中AA是Alpha通道, RR是红色通道, GG是绿色通道, BB是蓝色通道。

I'm assuming that 20% transparent means 80% opaque. 我假设20%的透明意味着80%的不透明。 If you meant the other way, instead of CC use 33 which is the hexadecimal for 255 * 0.2 = 51 . CC使用CC代替CC 33的十六进制值255 * 0.2 = 51

In order to calculate the proper value for an alpha transparency value you can follow this procedure: 为了计算Alpha透明度值的正确值,您可以按照以下步骤操作:

  1. Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80 ) 给定透明度百分比,例如20%,您知道不透明百分比值为80%(这是100-20=80
  2. The range for the alpha channel is 8 bits ( 2^8=256 ), meaning the range goes from 0 to 255. alpha通道的范围是8位( 2^8=256 ),这意味着范围从0到255。
  3. Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. 将不透明百分比投影到Alpha范围内,即将范围(255)乘以百分比。 In this example 255 * 0.8 = 204 . 在此示例中255 * 0.8 = 204 Round to the nearest integer if needed. 根据需要舍入到最接近的整数。
  4. Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). 将以10为底的3中获得的值转换为十六进制(以16为底)。 You can use Google for this or any calculator. 您可以将Google用于此计算器或任何计算器。 Using Google, type "204 to hexa" and it will give you the hexadecimal value. 使用Google,输入“ 204 to hexa”,它将为您提供十六进制值。 In this case it is 0xCC . 在这种情况下,它是0xCC
  5. Prepend the value obtained in 4. to the desired color. 将在4.中获得的值添加到所需的颜色之前。 For example, for red, which is FF0000 , you will have CCFF0000 . 例如,对于红色,即FF0000 ,您将拥有CCFF0000

You can take a look at the Android documentation for colors . 您可以查看有关颜色的Android文档 。


#4楼

You can try to do something like: 您可以尝试执行以下操作:

textView.getBackground().setAlpha(51);

Here you can set the opacity between 0 (fully transparent) to 255 (completely opaque). 在这里,您可以将不透明度设置为0(完全透明)到255(完全不透明)之间。 The 51 is exactly the 20% you want. 51正是您想要的20%。


#5楼

Use the below code for black: 将以下代码用于黑色:

<color name="black">#000000</color>

Now if I want to use opacity then you can use the below code: 现在,如果我要使用不透明度,则可以使用以下代码:

 <color name="black">#99000000</color> <!-- 99 is for alpha and others pairs zero's are for R G B -->

And below for opacity code: and all opacity level here 以下是不透明度代码:以及此处的所有不透明度级别

Hex Opacity Values 十六进制不透明度值

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

If you always to forget what code for transparency then you must have to see below link and no worry about to remember anything regarding transparent code :- 如果您始终忘记透明的代码,则必须查看以下链接,而不必担心记住任何有关透明代码的内容:

https://github.com/duggu-hcd/TransparentColorCode https://github.com/duggu-hcd/TransparentColorCode

textviewHeader.setTextColor(Color.parseColor(ColorTransparentUtils.transparentColor10(R.color.border_color)));

#6楼

I have taken three Views. 我已经采取了三种看法。 In the first view I set full (no alpha) color, on the second view I set half (0.5 alpha) color, and on the third view I set light color (0.2 alpha). 在第一个视图中,我设置了全色(无Alpha)颜色,在第二个视图中,我设置了一半(0.5 Alpha)颜色,在第三个视图中,我设置了浅色(0.2 Alpha)。

You can set any color and get color with alpha by using the below code: 您可以使用以下代码来设置任何颜色并使用alpha获取颜色:

File activity_main.xml 文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools = "http://schemas.android.com/tools"android:layout_width = "match_parent"android:layout_height = "match_parent"android:gravity = "center"android:orientation = "vertical"tools:context = "com.example.temp.MainActivity" ><Viewandroid:id = "@+id/fullColorView"android:layout_width = "100dip"android:layout_height = "100dip" /><Viewandroid:id = "@+id/halfalphaColorView"android:layout_width = "100dip"android:layout_height = "100dip"android:layout_marginTop = "20dip" /><Viewandroid:id = "@+id/alphaColorView"android:layout_width = "100dip"android:layout_height = "100dip"android:layout_marginTop = "20dip" /></LinearLayout>

File MainActivity.java 文件MainActivity.java

public class MainActivity extends Activity {private View fullColorView, halfalphaColorView, alphaColorView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fullColorView = (View)findViewById(R.id.fullColorView);halfalphaColorView = (View)findViewById(R.id.halfalphaColorView);alphaColorView = (View)findViewById(R.id.alphaColorView);fullColorView.setBackgroundColor(Color.BLUE);halfalphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.5f));alphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.2f));}private int getColorWithAlpha(int color, float ratio) {int newColor = 0;int alpha = Math.round(Color.alpha(color) * ratio);int r = Color.red(color);int g = Color.green(color);int b = Color.blue(color);newColor = Color.argb(alpha, r, g, b);return newColor;}
}

Kotlin version: Kotlin版本:

private fun getColorWithAlpha(color: Int, ratio: Float): Int {return Color.argb(Math.round(Color.alpha(color) * ratio), Color.red(color), Color.green(color), Color.blue(color))
}

Done 完成

如何在Android上使背景20%透明相关推荐

  1. android wi-fi_如何在Android上限制计量Wi-Fi网络的背景数据

    android wi-fi We've talked a lot about how to manage mobile data on Android, but what if your home i ...

  2. 在Android上设置imageview的透明背景

    本文翻译自:Set transparent background of an imageview on Android I am using a web view in which I am addi ...

  3. android用什么更新应用程序,如何在Android上更新应用程序 教你如何更新安卓手机APP...

    您从Play商店下载的大多数Android应用程序都会出于各种原因而定期提供更新:添加功能,错误修复,提高安全性...了解如何在Android智能手机上管理应用程序以及如何使它们保持最新. 如果您想要 ...

  4. android手机录屏多少fps,如何在Android上以90fps或120fps的屏幕录制?

    [5G资讯网]Android智能手机每天都在开拓新的领域,应用程序也在以相同的速度发展.最近增加的90赫兹显示屏为智能手机上的更好游戏铺平了道路.在OnePlus 7T推出之前,90Hz显示屏仅限于游 ...

  5. 最近朋友圈晒足迹足迹刷屏_如何在Android上使用私人浏览隐藏您的足迹

    最近朋友圈晒足迹足迹刷屏 Private browsing allows you to surf the web without saving any information about your b ...

  6. 如何在Android上管理startActivityForResult?

    本文翻译自:How to manage startActivityForResult on Android? In my activity, I'm calling a second activity ...

  7. android 泰文ttf字体,如何在Android上正确显示泰语diactirics?

    简短的序言.泰语字母有可能出现在辅音上方的元音符号,并且在辅音上方也存在变音符号(DS).当元音和DS出现时,它们出现一个在另一个之上,所以元音被设置在辅音之上并且DS被设置在元音之上.如何在Andr ...

  8. 如何在 Android 上恢复删除屏幕截图/照片的四种方式

    以下是在 Android 上恢复丢失的屏幕截图的方法--在您的 Android 手机上打开 Google 相册.在左上角,单击三个垂直线,然后选择垃圾箱.单击您要取消删除的照片,然后单击恢复以将选定的 ...

  9. 如何在 Android 上自定义来电通知?带有代码示例

    您将从本文中学习如何在 Android 上进行从基本布局到高级布局的来电通知.使用我们的示例自定义通知屏幕. 今天,我们将处理来电通知:我们将从最简单和最简约的通知开始,并以采用非系统设计的全屏通知结 ...

最新文章

  1. Android设计模式系列(2)--SDK源码之观察者模式
  2. ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法
  3. 编写五子棋的完整python代码_python实现五子棋游戏(pygame版)
  4. Go的io/ioutil
  5. POJ2255Tree Recovery
  6. time、random以及序列化模块
  7. 站在Java的角度看LinkedList
  8. 沃特玛采集均衡模块_云原生在京东丨云原生时代下的监控:如何基于云原生进行指标采集?...
  9. PAT 乙级 1019. 数字黑洞 (20) Java版
  10. python filter
  11. HDU 3394 Railway(点双连通分量)
  12. 数值分析(9)-最小二乘法
  13. paip. everything exe 文件不能启动的解决
  14. Redis入门小案例
  15. 架构案例丨苏宁易购:商品详情系统架构设计
  16. 诚龙网刻PXE网刻出错解决
  17. 美团在O2O场景下的广告营销
  18. sharepoint FAST serach 设置
  19. html tooltips效果,html5tooltips.js – 一款轻量级的3D工具提示插件
  20. 如何利用Swagger生成统一格式的Responses

热门文章

  1. RabbitMQ消息队列:发布/订阅(Publish/Subscribe)
  2. 2729: [HNOI2012]排队
  3. php安全篇值过滤用户输入的人参数
  4. zookeeper源码分析之leader选举
  5. 开源 java CMS - FreeCMS1.9发布。
  6. leetcode -- Single Number
  7. moosefs即将发布新版
  8. CentOS第一次安装MySQL的完整步骤
  9. 【转】CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”...
  10. VC之CString,wchar_t,int,string,char*之间的转换