zoukankan      html  css  js  c++  java
  • Android Shape使用

    说明

    在Android开发中,使用shape可以很方便的帮我们画出想要的背景,相对于png图片来说,使用shape可以减少安装包的大小,而且能够更好的适配不同的手机。

    使用

    先贴出官网上的说明:

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape=["rectangle" | "oval" | "line" | "ring"] >
        <corners
            android:radius="integer"
            android:topLeftRadius="integer"
            android:topRightRadius="integer"
            android:bottomLeftRadius="integer"
            android:bottomRightRadius="integer" />
        <gradient
            android:angle="integer"
            android:centerX="integer"
            android:centerY="integer"
            android:centerColor="integer"
            android:endColor="color"
            android:gradientRadius="integer"
            android:startColor="color"
            android:type=["linear" | "radial" | "sweep"]
            android:useLevel=["true" | "false"] />
        <padding
            android:left="integer"
            android:top="integer"
            android:right="integer"
            android:bottom="integer" />
        <size
            android:width="integer"
            android:height="integer" />
        <solid
            android:color="color" />
        <stroke
            android:width="integer"
            android:color="color"
            android:dashWidth="integer"
            android:dashGap="integer" />
    </shape>

    这里面已经列出了所有的shape属性。


    android:shape=["rectangle" | "oval" | "line" | "ring"]
    这里可以看出,shape可以画四种图形,分别是:矩形(rectangle)、椭圆(oval)、线(line)、圆环(ring)。

    先上效果图:


    这里写图片描述

    矩形(rectangle)

    直角矩形:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="@color/colorPrimary"></solid>
    
    </shape>

    solid:填充颜色

    圆角矩形:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <corners android:radius="10dp"></corners>
    
        <solid android:color="@color/colorPrimary"></solid>
    
        <padding android:bottom="12dp"
            android:left="12dp"
            android:right="12dp"
            android:top="12dp"></padding>
    
    </shape>

    corners:圆角大小。

            android:radius="integer"
            android:topLeftRadius="integer"
            android:topRightRadius="integer"
            android:bottomLeftRadius="integer"
            android:bottomRightRadius="integer"

    android:radius:表示4个角的圆角大小;
    还可以分别设置四个角的,使用下面四个属性:android:topLeftRadius、android:topRightRadius、android:bottomLeftRadius、android:bottomRightRadius分别表示:左上、右上、左下、右下。

    <padding android:bottom="12dp"
            android:left="12dp"
            android:right="12dp"
            android:top="12dp"></padding>

    padding:设置内边距。

    无填充带边框:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <corners android:radius="10dp"></corners>
    
    
        <padding android:bottom="12dp"
            android:left="12dp"
            android:right="12dp"
            android:top="12dp"></padding>
    
        <stroke android:width="5dp"
             android:color="@color/colorAccent"></stroke>
    
    
    </shape>

    stroke
    android:width:边框大小
    android:color:边框颜色

    渐变:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="@color/colorPrimary"></solid>
    
        <padding android:bottom="12dp"
            android:left="12dp"
            android:right="12dp"
            android:top="12dp"></padding>
    
        <!--angle 渐变角度,0:左到右;90:下到上;180:右到左;270:上到下-->
        <gradient android:startColor="@android:color/white"
            android:endColor="@android:color/black"
            android:angle="0"></gradient>
    </shape>

    gradient:
    android:startColor:渐变起始颜色
    android:endColor:渐变结束颜色
    android:angle:渐变角度:0:左到右;90:下到上;180:右到左;270:上到下

    椭圆(oval)

    一般用来画圆。

    纯色的圆:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    
        <solid android:color="@color/colorPrimary"></solid>
    
        <size android:height="100dp"
            android:width="100dp"></size>
    
    </shape>

    size的height和width设置为一样大小就是一个圆了。
    然后直接使用solid填充颜色即可。

    渐变效果:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    
        <size android:height="100dp"
            android:width="100dp"></size>
    
        <gradient android:centerX="0.5"
            android:centerY="0.5"
            android:type="sweep"
            android:startColor="@color/colorPrimary"
            android:endColor="@color/colorAccent"></gradient>
    </shape>

    android:centerX:表示渐变的X轴起始位置,范围0-1,0.5表示圆心。
    android:centerY:表示渐变的Y轴起始位置,范围0-1,0.5表示圆心。
    android:type:渐变类型,有三种
    分别是:
    linear 线性渐变,默认的渐变类型
    radial 放射渐变,设置该项时,android:gradientRadius也必须设置
    sweep 扫描性渐变

    线(line)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
    
        <stroke
            android:width="1dp"
             android:color="@color/colorAccent"
             android:dashGap="3dp"
             android:dashWidth="4dp"></stroke>
    
        <size android:height="3dp"></size>
    </shape>

    线是居中显示的。
    android:width:填充颜色的高度
    android:dashGap:虚线间距宽度
    android:dashWidth:虚线宽度
    <size android:height="3dp"></size>:line的高度,size大小必须大于android:width

    圆环(ring)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="ring"
        android:useLevel="false"
        android:thickness="10dp">
        <!--useLevel需要设置为false-->
    
        <solid android:color="@color/colorAccent"></solid>
    
    
    </shape>

    android:thickness:圆环宽度

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="ring"
        android:useLevel="false"
        android:thickness="10dp">
        <!--useLevel需要设置为false-->
    
        <solid android:color="@color/colorAccent"></solid>
    
        <gradient android:startColor="@color/colorAccent"
            android:endColor="@color/colorPrimary"
            android:type="sweep"></gradient>
    
    </shape>

    以上只是简单的介绍了一下shape的用户,里面有很多属性还没有用到,需要大家自己去实践一下,写出来看到效果才能更好的理解。

    完整代码地址:https://github.com/fccaikai/ShapeDemo
    以上如果有什么不对的地方希望大家能提出来。

  • 相关阅读:
    数组小练习
    数组
    利用数组进行排序
    继承练习
    黄金分割点
    百万富翁-循环练习
    SelectedIndexChanged事件, SelectedValueChanged事件和SelectionChangeCommitted事件的区别及应用——c#
    进制转换以及与字符串之间转换——c#
    文件中的类都不能进行设计,因此未能为该文件显示设计器
    winfrom自绘窗体边框——c#
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6955630.html
Copyright © 2011-2022 走看看