2012-06-18 120 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>