How to apply transparency gradually in a LinearLayout

1

Good morning!

I'm having trouble finding some LinearLayout property to control transparency.

My situation is as follows I have a LinearLayout with a background image, and I also have HomeScrollViewObserver for all the XML in the onScrollChanged method of the HomeScrollViewObserver I need to apply a transparency effect or smooth the background image exchange.

Below is a snippet of code, this code is only exchanging the background image.

<LinearLayout
    android:id="@+id/ll_icons_path"
    android:layout_width="fill_parent"
    android:layout_height="900dp"
    android:background="@drawable/home_icons_path"
    android:orientation="vertical">

//Método que controla a rolagem da tela na vertical(y) e horizontal(x)
@Override
public void onScrollChanged(HomeScrollViewObserver scrollView, int x, int y, int oldx, int oldy) {
    //Coloca a imagem de fundo padrão (fundo branco) somente é alterado quando o valor y for menor ou igual a 5
    if (y <= 100) {
        ll_icons_path.setBackgroundResource(R.drawable.home_icons_path);
    } else {
        //Verifica se o valor de y é maior ou igual a 6 e muda imagem de fundo(fundo transparente)
        if (y >= 101){
            ll_icons_path.setBackgroundResource(R.drawable.home_icons_path_semfundo);

        }
    }
}
    
asked by anonymous 28.01.2016 / 14:10

1 answer

1

You can apply a background to the linear layout with gradient:

gradient_background.xml

<shape>
    <gradient
        android:angle="270"
        android:startColor="#000"
        android:endColor="#00000000"
        android:type="linear" />
</shape>

This is a gradient that starts in black and goes up to 100% transparent black, getting what you want.

Then just apply this background to the background of linearlayout

<LinearLayout
    android:id="@+id/ll_icons_path"
    android:layout_width="fill_parent"
    android:layout_height="900dp"
    android:background="@drawable/gradient_background"
    android:orientation="vertical">

Edition:

background_multiplo.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/background_image" />
    <item>
         <shape>
             <gradient
                 android:angle="270"
                 android:startColor="#000"
                 android:endColor="#00000000"
                 android:type="linear" />
         </shape>
   </item>
</layer-list>

With this background you can add a drawable as background and then the gradient. Your linear layout will look something like:

<LinearLayout
    android:id="@+id/ll_icons_path"
    android:layout_width="fill_parent"
    android:layout_height="900dp"
    android:background="@drawable/background_multiplo"
    android:orientation="vertical">

If you want the image itself to disappear, then you have to change the image itself!

    
28.01.2016 / 14:14