I believe that you want your buttons to display different gradients when they are in normal state, pressed state, focused state, etc. This can be done using XML (create a selector.xml file in res / which refers to shape.xml files in res / drawable, each of which contains a gradient element, and then sets the background of your button to the selector.xml file that you created. ) However, the XML route will allow you to define gradients with two (or possibly three) static colors and without control over the location of the color breakpoints. The software solution will give you much greater flexibility and allow you to dynamically change colors. Here is an example Android project called GradientLab.
main.xml in res / layouts:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" style="@style/LayoutArea" > <TextView style="@style/LayoutRow" android:text="@string/hello" /> <Button style="@style/RowButton" android:id="@+id/btn1" android:text="1" /> <Button style="@style/RowButton" android:id="@+id/btn2" android:text="2" /> <Button style="@style/RowButton" android:id="@+id/btn3" android:text="3" /> </LinearLayout>
styles.xml in res / values:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="LayoutArea"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">fill_parent</item> </style> <style name="LayoutRow"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> </style> <style name="LayoutColumn"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">fill_parent</item> </style> <style name="LayoutItem"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style> <style name="RowButton" parent="LayoutRow"> <item name="android:layout_weight">1</item> <item name="android:layout_margin">8dp</item> <item name="android:gravity">center_horizontal</item> </style> </resources>
GradientLab.java in android.examples that demonstrates using gradients with buttons:
package android.example; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.Button; import android.widget.LinearLayout; public class GradientLab extends Activity {
UIGradientSelector.java in android.examples, which selects the gradient base in the button state:
package android.example; import android.graphics.drawable.StateListDrawable; public class UIGradientSelector extends StateListDrawable { public UIGradientSelector(int[] normalColors, int[] focusedColors, int[] pressedColors) { int stateFocused = android.R.attr.state_focused; int statePressed = android.R.attr.state_pressed; UIGradientDrawable normalGradient = new UIGradientDrawable(normalColors); UIGradientDrawable focusedGradient = new UIGradientDrawable( focusedColors); UIGradientDrawable pressedGradient; if (pressedColors == null) { pressedGradient = focusedGradient; } else { pressedGradient = new UIGradientDrawable(pressedColors); } addState(new int[] { stateFocused }, focusedGradient); addState(new int[] { statePressed }, pressedGradient); addState(new int[] { -statePressed, -stateFocused }, normalGradient); } }
UIGradientDrawable.java in android.examples that paints the surface:
package android.example; import android.graphics.drawable.PaintDrawable; import android.graphics.drawable.shapes.RectShape; public class UIGradientDrawable extends PaintDrawable { public UIGradientDrawable(int[] colors) { UIGradientShader gradientShader = new UIGradientShader(colors); setShape(new RectShape()); setCornerRadius(8); setShaderFactory(gradientShader); setDither(true); } }
UIGradientShader.java in android.examples files that handle the actual transitions:
package android.example; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Shader; import android.graphics.drawable.ShapeDrawable.ShaderFactory; public class UIGradientShader extends ShaderFactory { private int[] colors; private float[] positions; public UIGradientShader(int[] colors) { init(colors, null); } public UIGradientShader(int[] colors, float[] positions) { init(colors, positions); } private void init(int[] colors, float[] positions) { if (colors == null || colors.length == 0) { this.colors = new int[2]; this.colors[0] = Color.argb(255, 255, 255, 255); this.colors[1] = Color.argb(0, 0, 0, 0); } else if (colors.length == 1) { this.colors = new int[2]; this.colors[0] = colors[0]; this.colors[1] = Color.argb(0, 0, 0, 0); } else { this.colors = colors; } this.positions = positions; } public Shader resize(int width, int height) { LinearGradient lg = new LinearGradient(0, 0, 0, height, colors, positions, Shader.TileMode.REPEAT); return lg; } }