12

次のように、背景として使用するxmlで定義されたグラデーションドローアブルがあります。

<?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>

これをプログラムで実装する必要があります。私は次のようにGradientDrawableを使用しようとしました(このメソッドはカスタムビューに実装されています):

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

問題は、境界の設定が役に立たないか、(android:top、android:bottom xmlパラメーター)と同じように機能しないように見えることです。結果として得られる背景は、各レイヤーが上から下に上下にペイントされます。

私はこのようなものを生成したい:IMG

4

1 に答える 1

9

答えが見つかりました!。重複する可能性のあるマルチグラデーション形状

交換済み:

backColor.setBounds(0, 0,0, 4);
shadow.setBounds(0,98, 0, 0);

にとって

layerList.setLayerInset(0, 0, 0, 0, 4);
layerList.setLayerInset(1, 0, 98, 0, 0);     
于 2012-09-14T15:29:33.203 に答える