Android Shape使用
說明
在Android開發中,使用shape可以很方便的幫我們畫出想要的背景,相對于png圖片來說,使用shape可以減少安裝包的大小,而且能夠更好的適配不同的手機。
使用
先貼出官網上的說明:
<?xml version="1.0" encoding="utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <gradient android:angle="integer" android:centerX="integer" android:centerY="integer" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:useLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>這里面已經列出了所有的shape屬性。
從 android:shape=["rectangle" | "oval" | "line" | "ring"]
這里可以看出,shape可以畫四種圖形,分別是:矩形(rectangle)、橢圓(oval)、線(line)、圓環(ring)。
先上效果圖:
這里寫圖片描述
矩形(rectangle)
直角矩形:
solid:填充顏色
圓角矩形:
corners:圓角大小。
android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer"android:radius:表示4個角的圓角大小;
還可以分別設置四個角的,使用下面四個屬性:android:topLeftRadius、android:topRightRadius、android:bottomLeftRadius、android:bottomRightRadius分別表示:左上、右上、左下、右下。
padding:設置內邊距。
無填充帶邊框:
stroke
android:width:邊框大小
android:color:邊框顏色
漸變:
gradient:
android:startColor:漸變起始顏色
android:endColor:漸變結束顏色
android:angle:漸變角度:0:左到右;90:下到上;180:右到左;270:上到下
橢圓(oval)
一般用來畫圓。
純色的圓:
size的height和width設置為一樣大小就是一個圓了。
然后直接使用solid填充顏色即可。
漸變效果:
android:centerX:表示漸變的X軸起始位置,范圍0-1,0.5表示圓心。
android:centerY:表示漸變的Y軸起始位置,范圍0-1,0.5表示圓心。
android:type:漸變類型,有三種
分別是:
linear 線性漸變,默認的漸變類型
radial 放射漸變,設置該項時,android:gradientRadius也必須設置
sweep 掃描性漸變
線(line)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"> <stroke android:width="1dp" android:color="@color/colorAccent" android:dashGap="3dp" android:dashWidth="4dp"></stroke> <size android:height="3dp"></size> </shape>線是居中顯示的。
android:width:填充顏色的高度
android:dashGap:虛線間距寬度
android:dashWidth:虛線寬度
<size android:height="3dp"></size>:line的高度,size大小必須大于android:width
圓環(ring)
android:thickness:圓環寬度
以上只是簡單的介紹了一下shape的用戶,里面有很多屬性還沒有用到,需要大家自己去實踐一下,寫出來看到效果才能更好的理解。
完整代碼地址:https://github.com/fccaikai/ShapeDemo
以上如果有什么不對的地方希望大家能提出來。
總結
以上是生活随笔為你收集整理的Android Shape使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive 添加UDF(user defi
- 下一篇: linux之SQL语句简明教程---AN