Android y configuración de alfa para (imagen) ver alfa


¿No hay realmente un atributo XML equivalente a setAlpha(int)?

Si no, ¿qué alternativas existen?

Author: Rahul Tiwari, 2011-02-08

9 answers

No, no hay, vea cómo la sección "Atributos XML relacionados" falta en el ImageView .Documentación de setAlpha (int). La alternativa es usar View.setAlpha (float) cuya contraparte XML es android:alpha. Toma un rango de 0.0 a 1.0 en lugar de 0 a 255. Úselo, por ejemplo, como

<ImageView android:alpha="0.4">

Sin embargo, este último solo está disponible desde el nivel de API 11.

 132
Author: sschuberth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-09 10:39:13

Es más fácil que la otra respuesta. Hay un valor xml alpha que toma valores dobles.

android:alpha="0.0" eso es invisible

android:alpha="0.5" ver-a través

android:alpha="1.0" full visible

Así es como funciona.

 211
Author: jfcogato,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-15 18:43:07

No estoy seguro del XML, pero puede hacerlo por código de la siguiente manera.

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

En pre-API 11:

  • el rango es de 0 a 255 (inclusive), 0 siendo transparente y 255 siendo opaco.

En API 11+:

  • el rango es de 0f a 1f (inclusive), 0f siendo transparente y 1f siendo opaco.
 43
Author: Umesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-28 10:31:55

Tal vez una alternativa útil para un fondo de color liso:

Ponga un LinearLayoutsobre el ImageViewy use el LinearLayout como filtro de opacidad. En el siguiente un pequeño ejemplo con un fondo negro:

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

Variar la android:background atributo de la LinearLayout, entre #00000000 (totalmente transparente) y #FF000000 (totalmente opaco).

 12
Author: marnaish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-23 12:06:39

Ahora hay una alternativa XML:

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

Es: android:alfa="0.7"

Con un valor de 0 (transparente) a 1 (opaco).

 7
Author: Sorcerer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-05 18:59:49

Use android:alpha=0.5 para lograr la opacidad del 50% y convertir los iconos de Android Material de Negro a Gris.

 4
Author: Sachiin Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-16 14:47:47

Utilice este formulario para la versión antigua de 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
Author: Rafael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-30 19:28:26

El alfa se puede establecer junto con el color usando el siguiente formato hexadecimal #ARGB o #AARRGGBB. Véase http://developer.android.com/guide/topics/resources/color-list-resource.html

 0
Author: Grant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-19 00:47:11

setAlpha(int) está en desuso a partir de API 16: Android 4.1

Por favor use setImageAlpha(int) en su lugar

 0
Author: Alen Lee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-23 08:25:43