日常开发中少不了自己画shape,那么接下来就通过几个小案例来学习一下吧
效果图
按照上图先后顺序的效果
圆角矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--圆形shape-->
<!-- 填充颜色 -->
<solid android:color="#B848FF"></solid>
<!--线的宽度,颜色灰色-->
<stroke
android:width="2dp"
android:color="#777777"></stroke>
<!-- 矩形的圆角半径 -->
<corners android:radius="5dp" />
</shape>
圆角矩形虚线
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--圆角矩形虚线-->
<!-- 填充颜色 -->
<solid android:color="#FFFFFF"></solid>
<!-- 线的宽度,颜色灰色 -->
<stroke
android:width="1dp"
android:color="#D5D5D5"
android:dashGap="3dp"
android:dashWidth="10dp"></stroke>
<!-- 矩形的圆角半径 -->
<corners android:radius="10dp" />
</shape>
注:破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线
圆角矩形背景渐变
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--半圆角渐变-->
<!--分別對應上面左圆角的半径,上面右圆角的半径,下面左圆角的半径,下面右圆角的半径-->
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<!--设置渐变-->
<gradient
android:angle="0"
android:endColor="#FFFFFF"
android:startColor="#D5D5D5" />
<!--即android:angle=“0”时,是从左到右,按照开始颜色到结束颜色来渲染 的,
android:angle=“90”是从上到下来渲染的,android:angle=“180”是从右到左来渲染 的,
android:angle=“360”和android:angle=“0”是一样的,所以这里应该是这样的,
渲染时按照最原始的渲染色板(把控件内部看作一块可以绕中心旋转的板子)围绕控
件中心来旋转相应的度数,即android:angle里面的值就是所需要旋转的角度,
只是这个旋转角度必须是45的整数倍-->
<stroke
android:width="1dp"
android:color="#D5D5D5" />
</shape>
圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--圆形shape-->
<!-- 填充颜色 -->
<solid android:color="#B848FF"></solid>
<!--线的宽度,颜色灰色-->
<stroke
android:width="2dp"
android:color="#777777"></stroke>
<!-- 矩形的圆角半径 -->
<corners android:radius="5dp" />
</shape>
注:这里需要主要的是,在使用圆形shape时,必须将控件宽度和高度设置为一样
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.drawableshapoe.MainActivity">
<TextView
android:layout_width="200dp"
android:layout_height="25dp"
android:background="@drawable/rounded_rectangle_shape"
android:gravity="center_vertical|center_horizontal"
android:text="Hello World!" />
<TextView
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:background="@drawable/rounded_rectangle_dotted_line_shape"
android:gravity="center_vertical|center_horizontal"
android:text="Hello World!" />
<TextView
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:background="@drawable/rounded_half_rectangle_gradient_shape"
android:gravity="center_vertical|center_horizontal"
android:text="Hello World!" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:background="@drawable/rounded_oval_shape"
android:gravity="center_vertical|center_horizontal"
android:text="Hello World!" />
</LinearLayout>
源码
github:https://github.com/Waylenw/AndroidBase