Android Gradient programowalny
Mam zdefiniowany gradient w xml, którego używam jako tła, w ten sposób:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="4dp">
<shape>
<gradient
android:startColor="@color/blue"
android:endColor="@color/dark_blue"
android:angle="270" />
</shape>
</item>
<item android:top="98dp">
<shape>
<gradient
android:startColor="@color/black"
android:endColor="@color/transparent_black"
android:angle="270" />
</shape>
</item>
</layer-list>
Muszę to zaimplementować programowo. Próbowałem użyć GradientDrawable w następujący sposób (ta metoda jest zaimplementowana na widoku niestandardowym):
int[] colors1 = {getResources().getColor(R.color.black), getResources().getColor(R.color.trasparent_black)};
GradientDrawable shadow = new GradientDrawable(Orientation.TOP_BOTTOM, colors1);
shadow.setBounds(0,98, 0, 0);
int[] colors = new int[2];
colors[0] = getResources().getColor(R.color.blue);
colors[1] = getResources().getColor(R.color.dark_blue);
GradientDrawable backColor = new GradientDrawable(Orientation.TOP_BOTTOM, colors);
backColor.setBounds(0, 0,0, 4);
//finally create a layer list and set them as background.
Drawable[] layers = new Drawable[2];
layers[0] = backColor;
layers[1] = shadow;
LayerDrawable layerList = new LayerDrawable(layers);
setBackgroundDrawable(layerList);
Problem polega na tym, że wydaje się, że ustawienie granic jest bezużyteczne lub nie działa w taki sam sposób jak (android: top, android: dolne parametry xml). Powstałe tło to każda warstwa malowana od góry do dołu, jedna nad drugą.
Chcę wygenerować coś takiego:IMG http://i49.tinypic.com/2nueqf.png