Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use multiple gradients in one Drawable XML?

Tags:

android

Currently I have 3 drawable XML files defining 3 separate gradients. these gradients are dynamically set as the background color of an imageView in my code (which is working fine).

example: drawable\morningsky.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:bottom="4dp">
  <shape>
     <gradient
        android:startColor="@color/blue"
        android:endColor="@color/dark_blue"
        android:angle="270" />
  </shape>

example: drawable\eveningsky.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:bottom="4dp">
  <shape>
     <gradient
        android:startColor="@color/orange"
        android:endColor="@color/yellow"
        android:angle="270" />
  </shape>

I am setting the backgrounds in my imageView this way:

iv.setBackgroundResource(R.drawable.morningsky);

All is good, but do I really need to use multiple different drawable resource files for each gradient? Is there any way I can define all gradients in one single drawable file and then load that gradient from my code?

like image 901
Bluemarble Avatar asked Nov 01 '25 18:11

Bluemarble


2 Answers

you can use something like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:type="radial"
                android:gradientRadius="300"
                android:startColor="@android:color/white"
                android:endColor="@android:color/transparent"
                android:centerX="0.25"
                android:centerY="0.5"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:type="radial"
                android:gradientRadius="300"
                android:startColor="@android:color/white"
                android:endColor="@android:color/transparent"
                android:centerX="0.75"
                android:centerY="0.5"/>
        </shape>
    </item>
</layer-list>
like image 76
Juis Kel Avatar answered Nov 04 '25 10:11

Juis Kel


You can do it programatically :

public void setShadeBackground(View v){
    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                    new int[] {
                            0xFF2583ED,
                            0xFF8220C9,
                            0xFFBC0BCC,
                            0xFFD625ED ,
                            0xFFD407AE ,
                            0xFFF2115C }, //substitute the correct colors for these
                    new float[] {
                            0, 0.30f,0.50f, 0.60f,0.70f, 1 },
                    Shader.TileMode.REPEAT);
            return linearGradient;
        }
    };
    PaintDrawable paint = new PaintDrawable();
    paint.setShape(new RectShape());
    paint.setShaderFactory(shaderFactory);
    v.setBackgroundDrawable((Drawable)paint);

}
like image 41
Shrini Jaiswal Avatar answered Nov 04 '25 08:11

Shrini Jaiswal