Android and setting alpha for (image) view alpha

后端 未结 9 1351
终归单人心
终归单人心 2020-11-29 00:20

Is there really no XML attribute counterpart to setAlpha(int)?

If not, what alternatives are there?

相关标签:
9条回答
  • 2020-11-29 00:27

    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).

    0 讨论(0)
  • 2020-11-29 00:30

    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.

    0 讨论(0)
  • 2020-11-29 00:38

    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).

    0 讨论(0)
  • 2020-11-29 00:40

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

    0 讨论(0)
  • 2020-11-29 00:43

    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);
    
    0 讨论(0)
  • 2020-11-29 00:44

    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

    0 讨论(0)
提交回复
热议问题