Android Drawable之Shape使用小案例(一)

日常开发中少不了自己画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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值