Android and alpha setting for (image) alpha view

Actually there is no XML attribute for setAlpha(int) ?

If not, what are the alternatives?

+75
android android-layout xml alpha android-imageview
Feb 08 2018-11-11T00:
source share
8 answers

No, no, see how the Related XML Attributes section is missing from the ImageView.setAlpha (int) documentation. An alternative is to use View.setAlpha (float) , an XML analog of android:alpha . It takes a range from 0.0 to 1.0 instead of 0 to 255. Use it, for example. as

 <ImageView android:alpha="0.4"> 

However, the latter is only available from API level 11.

+83
Jan 23 '13 at 12:04 on
source share

This is simpler than the other answer. There is an xml alpha value that takes double values.

android:alpha="0.0" thats invisible

android:alpha="0.5" pass-through

android:alpha="1.0" full visibility

How it works.

+197
May 7 '13 at 10:55
source share

I'm not sure about XML, but you can do it using code as follows.

 ImageView myImageView = new ImageView(this); myImageView.setAlpha(xxx); 

In pre-API 11:

  • the range is from 0 to 255 (inclusive), 0 is transparent, and 255 is opaque.

In API 11 +:

  • range from 0f to 1f (inclusive), 0f transparent and 1f opaque.
+38
Feb 08 2018-11-11T00:
source share

Perhaps a useful alternative for a plain color background:

Place LinearLayout on top of ImageView and use LinearLayout as an opacity filter. Below is a small example with a black background:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF000000" > <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_stop_big" /> <LinearLayout android:id="@+id/opacityFilter" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#CC000000" android:orientation="vertical" > </LinearLayout> </RelativeLayout> 

Change the android: background LinearLayout attribute between # 00000000 (fully transparent) and # FF000000 (completely opaque).

+12
Feb 06 '12 at 19:24
source share

There is currently an alternative to XML:

  <ImageView android:id="@+id/example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/example" android:alpha="0.7" /> 

This: Android: Alpha = "0.7"

The value is from 0 (transparent) to 1 (opaque).

+7
Aug 05
source share

use android: alpha = 0.5 to achieve an opacity of 50% and turn the Android Material icons from black to gray.

+4
Sep 16 '16 at 14:47
source share

Use this form for an ancient version of Android.

 ImageView myImageView; myImageView = (ImageView) findViewById(R.id.img); AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F); alpha.setDuration(0); alpha.setFillAfter(true); myImageView.startAnimation(alpha); 
+3
Jul 30 '13 at 19:28
source share

Alpha can be set with color using the following hexadecimal format #ARGB or #AARRGGBB. See http://developer.android.com/guide/topics/resources/color-list-resource.html

0
Feb 19 2018-11-11T00:
source share



All Articles