Android Shape应用

1. Shape的形状有四种

android:shape="rectangle|oval|ring|line" 指定形状

<shape
    android:shape="rectangle" >
    <solid android:color="#ffff8c00" />
</shape>

<shape
    android:shape="oval">
    <solid android:color="#ffffc800"/>
</shape

<shape
    android:shape="ring"
    android:thickness="5dp"
    android:useLevel="false">
    <solid android:color="#ffff8800" />
</shape>

<shape
    android:shape="line" >
    <stroke
        android:width="5dp"
        android:color="#ffff8c00" />
</shape>

效果如下
在这里插入图片描述

2. solid

retangleovalring都适用solid来填充颜色。

  • android:color用来指定颜色。

3. stroke

  • android:width指定线的宽度。
  • android:color指定线的颜色。
  • android:dashWidth指定虚线的宽度。
  • android:dashGap指定虚线的间隔。

示例代码

<shape
    android:shape="rectangle" >
    <solid android:color="#ffff8c00" />
    <stroke
        android:width="2dp"
        android:color="#ff000000" />
</shape>

<shape
    android:shape="rectangle" >
    <stroke
        android:width="2dp"
        android:dashWidth="10dp"
        android:dashGap="5dp"
        android:color="#ffff8c00" />
</shape>

效果如下
在这里插入图片描述

4. corners

  • android:radius指定四个角的半径
  • android:topLeftRadius指定左上角的半径
  • android:topRightRadius指定右上角的半径
  • android:bottomLeftRadius指定左下角的半径
  • android:bottomRightRadius指定右下角的半径

示例代码

<corners
    android:radius="10dp" />

<corners
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />

效果如下
在这里插入图片描述

5. gradient

  • android:startColor指定开始颜色
  • android:endColor指定结束颜色
  • android:type="linear | radial | sweep"指定颜色渐变的方式
    示例代码
    <gradient 
        android:startColor="#ffff8c00"
        android:endColor="#ffffffff"
        android:type="linear" />
    
    <gradient
        android:startColor="#ffff8c00"
        android:endColor="#ffffffff"
        android:type="radial"
        android:gradientRadius="60"/>
    
    <gradient
        android:startColor="#ffff8c00"
        android:endColor="#ffffffff"
        android:type="sweep" />
    
    效果如下
    在这里插入图片描述
  • android:gradientRadius指定radius的半径
  • android:angle指定方向,0代表从左往右,90代表从下到上,180代表从有往左,angle必须是45的倍数
    效果如下
    在这里插入图片描述
    gradient同样可以用于其他形状
    在这里插入图片描述

6. ring的专用属性

  • android:innerRadius指定内环的半径
  • android:innerRadiusRatio环的宽度比率表示内环的半径,5时为占比0.2
  • android:thickness指定环的厚度
  • android:thicknessRatio环的宽度比率来表示环的厚度,5时为占比0.2
  • android:useLevelfalse

示例代码

<shape
    android:shape="ring"
    android:thickness="5dp"
    android:useLevel="false"/>

<shape
    android:shape="ring"
    android:thickness="10dp"
    android:innerRadius="20dp"
    android:useLevel="false"/>

<shape
    android:shape="ring"
    android:thickness="10dp"
    android:innerRadius="15dp"
    android:useLevel="false" />

<shape
    android:shape="ring"
    android:thicknessRatio="5"
    android:innerRadius="15dp"
    android:useLevel="false"/>

效果如下
在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值