但是如果每个数字都叫美工做一遍无疑是不现实的(除非这里需要“自定义字体”)
所以下面来讲述一下如何适应shape来实现上述图片
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false" >
<solid
android:color="#f00" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
<solid
android:color="#f00" />
<stroke
android:width="10px"
android:color="#000"
android:dashWidth="20dp"
android:dashGap="10dp"/>
<size
android:width="300dp"
android:height="300dp" />
<gradient
android:startColor="#b77"
android:endColor="#f00"
android:angle="225" />
</shape>
讲解一下:
(1)android:shape="oval" Defines the type of shape. Valid values are "rectangle" "oval" "line" "ring"
意思就是当前shape的形状,这里设置为椭圆形(圆形)
(2)android:useLevel Boolean,if this is used as a LevelListDrawable
指的是是否使用LevelListDrawable。LeveListDrawable管理着一组交替的drawable资源,用level-list标签的xml来说实现,然后用imageView.getDrawable().setLevel(i)来改变当前drawable资源。
(3)<solid> A solid color to fill the shape.
指的是图形填充的颜色
(4)<stroke> A stroke line for the shape.
指的是图形轮廓的颜色android:dashWidth表示虚线宽度android:dashGap表示虚线之间的空隙(间隔)
(5)<gradient> Specifies a gradient color for the shape.
指定一个渐变颜色的形状
layout的布局是这样的
<span style="font-size: 18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaa">
<TextView
android:layout_centerInParent="true"
android:layout_width="250dp"
android:layout_height="250dp"
android:gravity="center"
android:textSize="220sp"
android:textColor="#fff"
android:background="@drawable/test_shape"
android:text="7"/>
</RelativeLayout></span></span>
最终得出如下结果图:
总结:
使用shape的优点:
(1)减少安装包的体积。
(2)提高加载速度。android中解析xml并绘制的速度比直接绘制图片的速度要快