我正在使用线性布局和框架布局。在线性布局中,我将一张图片作为背景,在框架布局中,我放置了一个imageView。在该imageView中,我添加了一张图片。
现在我想使第二张图片(也就是在imageView中的那张图片)透明。我该怎么做?
我正在使用线性布局和框架布局。在线性布局中,我将一张图片作为背景,在框架布局中,我放置了一个imageView。在该imageView中,我添加了一张图片。
现在我想使第二张图片(也就是在imageView中的那张图片)透明。我该怎么做?
试试这个:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
注意: setAlpha(int)
已经被弃用,现在应该使用setAlpha(float)
,其中0表示完全透明,1表示完全不透明。使用方式如下:myImage.setAlpha(0.5f)
android:alpha
在XML中实现以下效果:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blah"
android:alpha=".75"/>
<ImageView android:id="@+id/myImage"
ImageView myImage = (ImageView) findViewById(R.id.myImage);
然后,将可见性设置为 GONE:
myImage.setVisibility(View.GONE);
myImage.setVisibility(View.VISIBLE);
int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);
android:background="@null"
使用setAlpha(float alpha)
设置透明度。下面的代码对我有效,其中使用了0-1之间的浮点alpha值。
1:完全不透明
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources[position]); imageView.setAlpha(.80f);
由于 setAlpha int 方法已被弃用,现在可以使用 setImageAlpha (int) 方法。
ImageView img = (ImageView) findViewById(R.id.img_image);
img.setImageAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
类型为ImageView的方法setAlpha(int)
已经过时。
建议使用
image.setImageAlpha(127);
//value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
在 ImageView 中,仅设置不透明度会使图像模糊,尝试添加 tint 属性。
android:tint="#66000000"
imageView.setColorFilter(R.color.transparent);
在您的colors.xml中需要定义透明颜色。
<color name="transparent">#66000000</color>
在XML中,使用:
android:background="@android:color/transparent"