2014-01-30 34 views
0

我学会了如何使用图层列表为textview /按钮创建阴影效果。我用这个代码,以供参考: -如何以编程方式从图层列表创建阴影效果?

<?xml version="1.0" encoding="utf-8"?> 

 <corners android:radius="15dp" /> 
    </shape> 
</item> 
<!-- background color --> 
<item 
    android:bottom="3px" 
    android:left="3px" 
    android:right="3px" 
    android:top="3px"> 
    <shape android:shape="rectangle" > 
     <solid android:color="#cc2b2b" /> 


     <corners android:radius="8dp" /> 
    </shape> 
</item> 
<!-- over left shadow --> 
<item> 
    <shape android:shape="rectangle" > 
     <gradient 
      android:angle="180" 
      android:centerColor="#00FF0000" 
      android:centerX="0.9" 
      android:endColor="#99000000" 
      android:startColor="#00FF0000" /> 

     <corners android:radius="8dp" /> 
    </shape> 
</item> 
<!-- over right shadow --> 
<item> 
    <shape android:shape="rectangle" > 
     <gradient 
      android:angle="360" 
      android:centerColor="#00FF0000" 
      android:centerX="0.9" 
      android:endColor="#99000000" 
      android:startColor="#00FF0000" /> 

     <corners android:radius="8dp" /> 
    </shape> 
</item> 
<!-- over top shadow --> 
<item> 
    <shape android:shape="rectangle" > 
     <gradient 
      android:angle="-90" 
      android:centerColor="#00FF0000" 
      android:centerY="0.9" 
      android:endColor="#00FF0000" 
      android:startColor="#99000000" 
      android:type="linear" /> 

     <corners android:radius="8dp" /> 
    </shape> 
</item> 
<!-- over bottom shadow --> 
<item> 
    <shape android:shape="rectangle" > 
     <gradient 
      android:angle="90" 
      android:centerColor="#00FF0000" 
      android:centerY="0.9" 
      android:endColor="#00FF0000" 
      android:startColor="#99000000" 
      android:type="linear" /> 

     <corners android:radius="8dp" /> 
    </shape> 
</item> 

enter image description here

我不知道如何来实现这个动态使用图层绘制对象/渐变绘制对象? 任何建议

回答

1

这会是一个痛苦的缓慢的过程,但肯定完全可能的,它只是下面正是XML做:

// create the layers 
Drawable[] layer = new Drawable[5] // or whatever amount in the XML 

// build the layers 
layer[0] = new ShapeDrawable(); 
layer[0]. // call methods here to configure the 1st layer 

layer[1] = new ShapeDrawable(); 
layer[1]. // call methods here to configure the 2nd layer 
... etc ... 

LayerDrawable layerDrawable = new LayerDrawable(layer); 

引用LayerDrawableShapeDrawableDrawables

相关问题