在Android上设置imageview的透明背景

本文介绍了如何在Android上将ImageView的背景设置为透明,包括通过代码设置、使用资源ID解析以及调整颜色透明度的方法。还提到了Android内置的R.color.transparent常量和使用透明PNG图像来实现效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Set transparent background of an imageview on Android

I am using a web view in which I am adding an image view. 我正在使用Web视图,我在其中添加图像视图。 How can I set the background of this image view to transparent? 如何将此图像视图的背景设置为透明?

I have tried this: 我试过这个:

mImageview.setBackgroundResource(R.color.trans);

Where trans<color name="trans">#00000000 </color> . trans<color name="trans">#00000000 </color>


#1楼

参考:https://stackoom.com/question/6GHS/在Android上设置imageview的透明背景


#2楼

如果您想在代码中使用它,只需:

mComponentName.setBackgroundColor(Color.parseColor("#80000000"));

#3楼

或者,作为备用,使用以下代码解析资源ID:

  mComponentName.setBackgroundColor(getResources().getColor(android.R.color.transparent));

#4楼

In addition to what Harshad mentioned: 除了哈尔沙德提到的:

Two hexadecimal characters can be appended to any hexadecimal color code. 两个十六进制字符可以附加到任何十六进制颜色代码。 The first two characters in an 8-digit hex color code represents its opacity in Android. 8位十六进制颜色代码中的前两个字符表示其在Android中的不透明度。

The two hexadecimal characters can range from 00 to FF. 两个十六进制字符的范围为00到FF。 For example, 例如,

  • Normal opaque black hex- "#000000" 普通不透明黑色六角形 - “#000000”
  • Fully transparent - "#00000000" 完全透明 - “#00000000”
  • Fully opaque - "#FF000000" 完全不透明 - “#FF000000”
  • 50% transparent - "#7F000000" 50%透明 - “#7F000000”

This way you can change any color to any level of transparency. 这样,您可以将任何颜色更改为任何透明度级别。

To find the hexadecimal prefix from a percentage: 要从百分比中查找十六进制前缀:

Divide the percentage number by 100 and multiply by 255 to get the decimal value. 将百分比数除以100并乘以255得到小数值。 Convert the decimal to hexadecimal here . 这里将十进制转换为十六进制。

For example, for 50%, 50/100 * 255 = 127. Using the link we get hexadecimal value 7F. 例如,对于50%,50/100 * 255 = 127.使用该链接我们得到十六进制值7F。

Source: Android: how to create a transparent or opaque background 来源: Android:如何创建透明或不透明的背景


#5楼

There is already a transparent built into Android: R.color.transparent. Android内置了一个透明的:R.color.transparent。 http://developer.android.com/reference/android/R.color.html#transparent http://developer.android.com/reference/android/R.color.html#transparent

But I think you may want to make the background of the image that you are placing into the WebView transparent, for example, with a transparent PNG, rather than the ImageView background. 但我认为您可能希望将放置到WebView中的图像的背景透明化,例如,使用透明的PNG,而不是ImageView背景。 If the actual image is not at all see-through then the ImageView background can't be seen through it. 如果实际图像根本没有透视,则无法通过它看到ImageView背景。


#6楼

You could also use View.setAlpha(float) to change the visibility precisely. 您还可以使用View.setAlpha(float)精确更改可见性。

0 would be transparent, 1 fully visible. 0将是透明的,1是完全可见的。 ;) ;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值