Android中shape的使用
生活随笔
收集整理的這篇文章主要介紹了
Android中shape的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
引言
Android中常常需要用到一些邊框、背景之類的素材,而這些很多時候不是通過美工人員P圖得到的,而是通過shape標(biāo)簽來實(shí)現(xiàn)的。今天就探究一下shape到底怎么用以及相關(guān)的一些特性。
shape的定義
本來想在網(wǎng)上找點(diǎn)簡單易懂的中文解釋,搜了半天沒看到,于是乎就上Android官網(wǎng)上看了看,官網(wǎng)的解釋是:
An XML file that defines a geometric shape, including colors and gradients.- 1
- 1
大概意思就是:一種定義形狀的xml文件,包括顏色、漸變。其實(shí)還包括圓角、內(nèi)間距、邊框等。總之是一種用于定義形狀資源的文件。
shape使用
舉一個小例子先看看
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><!-- 邊框 --><stroke android:width="2dp"android:color="#FFFFFF"/><!-- 圓角 --><corners android:radius="8dp"/><!--填充--><solid android:color="#00000000"/> </shape>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
作為背景用于TextView
android:background="@drawable/recetegle_textview"- 1
- 1
效果:
shape屬性詳解
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><!--屬性--> </shape>- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
這是shape文件的基本格式。?
根標(biāo)簽是shape,其中屬性:android:shape="……"是必要屬性,它規(guī)定shape的基本形狀。
android:shape
它可選的值如下:
rectangle //矩形 oval //橢圓 line //線 ring //圓環(huán)- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
android:shape=ring時的特殊屬性
android:innerRadius="dimension" //內(nèi)環(huán)的半徑。 android:innerRadiusRatio="float" //這個值表示內(nèi)部環(huán)的比例,例如,如果android:innerRadiusRatio = " 5 ",那么內(nèi)部的半徑等于環(huán)的寬度除以5。這個值會被android:innerRadius重寫。 默認(rèn)值是9。 android:thickness="dimension" //環(huán)的厚度. android:thicknessRatio="float" //厚度的比例。例如,如果android:thicknessRatio= " 2 ",然后厚度等于環(huán)的寬度除以2。這個值是被android:innerRadius重寫, 默認(rèn)值是3。 android:useLevel="boolean" //如果用在 LevelListDrawable里,那么就是true。如果通常不出現(xiàn)則為false。- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
子屬性
自屬性是包含在shape內(nèi)部的一些屬性,主要包括:
corners //圓角 gradient//漸變 padding //內(nèi)邊距 size //大小 solid //填充 stroke //邊框- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
corners 圓角
<cornersandroid:radius="dimension" //圓角,比如8dp、5dp等等android:topLeftRadius="dimension" //左上圓角,比如8dp、5dp等等android:topRightRadius="dimension" //右上圓角,比如8dp、5dp等等android:bottomLeftRadius="dimension" //左下圓角,比如8dp、5dp等等android:bottomRightRadius="dimension" />//左下圓角,比如8dp、5dp等等- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
gradient漸變
<gradientandroid:angle="integer" //漸變角度,默認(rèn)為0,即從左向右,90為從下向上。值必須是45的倍數(shù)android:centerX="float" //漸變中心X,相對位置與整個shape的X的相對位置,取值范圍0.0~1.0 android:centerY="float" //同X android:centerColor="color" //介于end和start之間的顏色android:endColor="color" //結(jié)束顏色 android:gradientRadius="integer" //漸變半徑,只有當(dāng)type="radial"時生效 android:startColor="color" //開始顏色android:type="linear" //漸變模式,可選的值有:linear線型,radial放射型,sweep范圍型(這個真不知道怎么翻譯)android:useLevel="boolean" /> //使用級別,默認(rèn)為false,當(dāng)用于levelListDrawable時設(shè)為true。- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
padding 內(nèi)邊距
<paddingandroid:left="dimension" //左android:top="dimension" //上android:right="dimension" //左 android:bottom="dimension" /> //下- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
size 大小
<sizeandroid:width="dimension" //寬android:height="dimension" /> //高- 1
- 2
- 3
- 1
- 2
- 3
solid 填充
<solidandroid:color="color" /> //填充顏色- 1
- 2
- 1
- 2
stroke 邊框
<strokeandroid:width="dimension" //邊框?qū)?/span>android:color="color" //邊框顏色android:dashWidth="dimension" //虛線寬度,只有當(dāng)設(shè)置了dashGap時有效android:dashGap="dimension" /> //虛線間距,只有當(dāng)設(shè)置了dashWidth時有效- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
ShapeDrawable
ShapeDrawable是一個Drawable的子類用與繪制原始的shape。這里不再展開了。?
需要詳細(xì)了解的同學(xué)看這里
總結(jié)
shape是一個很好的繪制圖片的工具,如果用好了,可以繪制出很棒的效果。?
我畫了個球,哈哈哈。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
總結(jié)
以上是生活随笔為你收集整理的Android中shape的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio相见恨晚的操
- 下一篇: 15个Android通用流行框架大全