2012-06-18 87 views
3

需要在视图中绘制“X”使用形状,但X的边缘必须锚定在视图的左侧,顶部,右侧和底部。Android:使用形状绘制“X”

事情是这样的:

enter image description here

+0

'ShapeDrawable'不太适合这个角色。你为什么觉得你需要使用它们?这看起来好像用一个自定义的'View'就可以简单得多,你可以在'Canvas'上画线。 – CommonsWare

回答

7

这将是通过创建自定义视图和重写的onDraw方法做要容易得多。 IE浏览器。

public class XView extends View { 

    @Override 
    public void onDraw(Canvas canvas) { 
     float width = getMeasuredWidth(); 
     float height = getMeasuredHeight(); 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     canvas.drawLine(0,0,width,height,paint); 
     canvas.drawLine(width,0,0,height,paint); 
    } 
} 
+0

好主意,但如果油漆是半透明的,则不起作用。 – bvdb

0

我有一个类似的情况,但我的盒子是使用预定义的高度和宽度,做到了只使用XML

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 

     <shape android:shape="rectangle"> 

      <solid android:color="@color/status_expired_color" /> 

     </shape> 

    </item> 

    <item android:top="2dp" 
     android:left="2dp" 
     android:right="2dp" 
     android:bottom="2dp"> 

     <shape android:shape="rectangle"> 
      <solid android:color="@color/white" /> 
     </shape> 

    </item> 

    <item android:top="14sp" 
     android:left="6dp" 
     android:right="6dp" 
     android:bottom="14sp"> 

     <rotate 
      android:fromDegrees="45" 
      android:toDegrees="45" 
      android:pivotX="50%" 
      android:pivotY="50%" > 

      <shape android:shape="rectangle"> 
       <solid android:color="@color/status_expired_color" /> 
      </shape> 

     </rotate> 

    </item> 

    <item android:top="6dp" 
     android:left="14sp" 
     android:right="14sp" 
     android:bottom="6dp"> 

     <rotate 
      android:fromDegrees="45" 
      android:toDegrees="45" 
      android:pivotX="50%" 
      android:pivotY="50%" > 

      <shape android:shape="rectangle"> 
       <solid android:color="@color/status_expired_color" /> 
      </shape> 

     </rotate> 

    </item> 

</layer-list> 

@color/status_expired_color = #E9510E 
@color/white = #ffffff 

输出地说:
enter image description here

0

由x混合物在抽拉:

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

<item> 
    <rotate 
     android:fromDegrees="135" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toDegrees="135"> 
     <shape android:shape="line"> 
      <stroke android:width="1dp" android:color="@color/social_grey" /> 
     </shape> 
    </rotate> 
</item> 
<item> 
    <rotate 
     android:fromDegrees="45" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toDegrees="45"> 
     <shape android:shape="line"> 
      <stroke android:width="1dp" android:color="@color/social_grey" /> 
     </shape> 
    </rotate> 
</item> 

随着填充:

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

<item 
    android:left="4dp" 
    android:right="4dp" 
    > 
    <rotate 
     android:fromDegrees="135" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toDegrees="135"> 
     <shape android:shape="line"> 
      <stroke android:width="1dp" android:color="@color/social_grey" /> 
     </shape> 
    </rotate> 
</item> 
<item 
    android:left="4dp" 
    android:right="4dp" 
    > 
    <rotate 
     android:fromDegrees="45" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toDegrees="45"> 
     <shape android:shape="line"> 
      <stroke android:width="1dp" android:color="@color/social_grey" /> 
     </shape> 
    </rotate> 
</item>