Is there really no XML attribute counterpart to setAlpha(int)
?
If not, what alternatives are there?
Maybe a helpful alternative for a plain-colored background:
Put a LinearLayout over the ImageView and use the LinearLayout as a opacity filter. In the following 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>
Vary the android:background attribute of the LinearLayout between #00000000 (fully transparent) and #FF000000 (fully opaque).
It's easier than the other response.
There is an xml value alpha
that takes double values.
android:alpha="0.0"
thats invisible
android:alpha="0.5"
see-through
android:alpha="1.0"
full visible
That's how it works.
There is now an XML alternative:
<ImageView
android:id="@+id/example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/example"
android:alpha="0.7" />
It is: android:alpha="0.7"
With a value from 0 (transparent) to 1 (opaque).
use android:alpha=0.5 to achieve the opacity of 50% and to turn Android Material icons from Black to Grey.
Use this form to 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);
The alpha can be set along with the color using the following hex format #ARGB or #AARRGGBB. See http://developer.android.com/guide/topics/resources/color-list-resource.html