Android 編程下 shape 繪製圖形

1. 使用 shape 繪製線條


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

    <!-- 顯示一條虛線,破折線的寬度爲 dashWith,破折線之間的空隙的寬度爲 dashGap,當 dashGap=0dp 時,爲實線 -->
    <stroke
        android:dashGap="3dp"
        android:dashWidth="2dp"
        android:width="1dp"
        android:color="#777777" />

    <!-- 虛線的高度 -->
    <size android:height="2dp" />

</shape>

2. 使用 shape 繪製圓形


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

   <!-- 填充顏色 -->
   <solid android:color="#F0F0F0" ></solid>

   <!--線的寬度,顏色灰色-->
   <stroke android:width="2dp" android:color="#777777"></stroke>

   <!-- 矩形的圓角半徑 -->
   <corners android:radius="5dp" />

</shape>

3. 使用 shape 繪製矩形


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

   <!-- 填充顏色 -->
   <solid android:color="#F0F0F0" ></solid>

   <!-- 顯示一條實線,線的寬度爲 width,顏色爲 color -->
   <!-- <stroke android:width="2dp" android:color="#E3E0D5"></stroke> -->

   <!-- 顯示一條虛線,破折線的寬度爲 dashWith,破折線之間的空隙的寬度爲 dashGap,當 dashGap=0dp 時,爲實線 -->
   <stroke
       android:dashGap="2dp"
       android:dashWidth="5dp"
       android:width="2dp"
       android:color="#777777" />

   <!-- 虛線的高度 -->
   <size android:height="10dp" />

   <!-- 矩形的圓角半徑 -->
   <corners android:radius="0dp" />

</shape>

4. 使用 shape 繪製半圓角矩形


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- topLeftRadius、topRightRadius 爲半圓角矩形上半部分的圓角半徑,bottomLeftRadius、bottomRightRadius 爲矩形下半部分的圓角半徑,值爲0表示直角 -->
    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    <gradient
        android:angle="270"
        android:endColor="#d3d3d3"
        android:startColor="#d3d3d3" />

    <stroke
        android:width="0.5dp"
        android:color="#d9d9d9" />

</shape>
發佈了25 篇原創文章 · 獲贊 14 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章