Android Drawable中shape的使用

xiaoxiao2021-02-28  133

ShapeDrawable ShapeDrawable是经常用到的Drawable,可以通过颜色构造图形,类似于矢量图,没有拉伸等问题。 ShapeDrawable一般通过<shape>标签来创建,有多个属性可以调节 android:shape   有四个类型,rectangle(矩形)、line(线)、ring(圆环)、oval(椭圆),默认为rectangle。 注意line要和<stroke>一起实用才有效果,例如画虚线 <solid>标签指定了填充的颜色,表示纯色填充,与<gradient>标签互斥,通过color属性指定颜色 <solid android:color="@android:color/holo_blue_bright" /> <corners>标签指定了圆角大小,针对rectangle设置。可以通过bottomLeftRadius、bottomRightRadius、topLeftRadius、topRightRadius分别设置不同的圆角, 也可以直接通过radius设置同一个圆角。注意在<corners>标签中radius优先级最低 <corners         android:bottomLeftRadius="5dp"         android:bottomRightRadius="5dp"         android:topLeftRadius="5dp"         android:topRightRadius="5dp" /> <stroke>标签表示描边。width设置描边的宽度,越大显示越粗。color设置描边的颜色。dashWidth设置绘制虚线的每段长度。dashGap设置绘制虚线间隔宽度 <stroke         android:width="1dp"         android:color="@android:color/holo_green_dark"         android:dashGap="3dp"         android:dashWidth="5dp"/> 例如:         <shape xmlns:android="http://schemas.android.com/apk/res/android"             android:shape="rectangle">             <corners                 android:bottomLeftRadius="5dp"                 android:bottomRightRadius="5dp"                 android:topLeftRadius="5dp"                 android:topRightRadius="5dp" />             <solid android:color="@android:color/holo_blue_bright" />             <stroke                 android:width="1dp"                 android:color="@android:color/holo_green_dark"                 android:dashGap="3dp"                 android:dashWidth="5dp"/>

        </shape>

当 android:shape 设置为ring(圆环)时,通过innerRadius、innerRadiusRatio、thickness、thicknessRatio设置圆环显示 innerRadius设置圆环内半径。innerRadiusRatio内半径占整个Drawable宽度的比例,如果设置,内半径 = 宽度/n,n默认为9。 thickness设置圆环厚度。thicknessRatio厚度占整个Drawable宽度的比例,如果设置,厚度 = 宽度/n,n默认为3。 innerRadius、innerRadiusRatio同时存在时,innerRadius为准。thickness、thicknessRatio同时存在时,thickness为准 例如 : <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="ring"     android:innerRadiusRatio="4"     android:thicknessRatio="4"     android:useLevel="false">     <solid android:color="@android:color/holo_blue_bright" />     <stroke         android:width="1dp"         android:color="@android:color/holo_green_dark"         android:dashGap="3dp"         android:dashWidth="5dp"/> </shape> 或者 <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="ring"     android:innerRadius="50dp"     android:thickness="50dp"     android:useLevel="false">     <solid android:color="@android:color/holo_blue_bright" />     <stroke         android:width="1dp"         android:color="@android:color/holo_green_dark"         android:dashGap="3dp"         android:dashWidth="5dp"/> </shape> 设置在View上: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">     <TextView         android:layout_width="200dp"         android:layout_height="200dp"         android:layout_centerInParent="true"         android:background="@drawable/shape_test"         android:text="测试"         android:gravity="center"         android:textSize="18sp" /> </RelativeLayout> android:tint表示着色,如果设置会覆盖掉<solid>、<gradient>标签效果 android:tintMode 表示着色类型,具体类型与图片混合类型PorterDuffXfermode相似,src_in|src_atop|src_over|add|multiply|screen 这两个属性运用较少,具体效果可以多尝试看看 <gradient>标签表示渐变效果,与<solid>标签互斥  android:angle设置渐变的角度,必须为45的倍数,0表示从左向右,90表示从下到上,270表示从上到下。角度变换效果按逆时针方向  android:startColor设置渐变起始颜色  android:centerColor设置渐变中间颜色  android:endColor设置渐变结束颜色  android:type设置渐变类型  linear(线性渐变),radial(径向渐变),sweep(扫描渐变),默认为linear。  android:gradientRadius设置渐变半径,只有type为radial才有效。  android:centerX设置渐变中心点横坐标相对位置,0-1.0之间  android:centerY设置渐变中心点纵坐标相对位置, 0-1.0之间  例如:  <shape xmlns:android="http://schemas.android.com/apk/res/android"      android:innerRadius="50dp"      android:shape="rectangle"      android:thickness="50dp"      android:useLevel="false">      <stroke          android:width="1dp"          android:color="@android:color/holo_green_dark"          android:dashGap="3dp"          android:dashWidth="5dp" />      <gradient          android:angle="270"          android:centerColor="@android:color/holo_orange_dark"          android:endColor="@android:color/holo_blue_bright"          android:startColor="@android:color/holo_blue_dark"          android:type="linear"          android:centerY="0.8"       />       <size               android:width="20dp"               android:height="20dp" />  </shape>  <size>标签设置Drawable的固有宽高。  对于一张图片Drawable而言getIntrinsicWidth、getIntrinsicHeight就是图片资源的固有宽度。  shape生成的Drawable没有固有宽高,可以通过<size>标签设置。作为View背景时,但是这不是它最终的宽高,会被拉伸或者缩小和View大小一致。
转载请注明原文地址: https://www.6miu.com/read-18520.html

最新回复(0)