Bluemarble
Bluemarble

Reputation: 2059

Can I use multiple gradients in one Drawable XML?

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?

Upvotes: 4

Views: 5145

Answers (3)

Juis Kel
Juis Kel

Reputation: 391

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>

Upvotes: 10

Shrini Jaiswal
Shrini Jaiswal

Reputation: 1090

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

}

Upvotes: 1

Dmitriy Puchkov
Dmitriy Puchkov

Reputation: 1601

You can use the selector

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true" android:drawable="@drawable\eveningsky.xml"/>
      <item android:state_selected="true" android:drawable="@drawable\morningsky.xml"/>
      <item android:drawable="@drawable\morningsky.xml"/>
   </selector>

Upvotes: 0

Related Questions