属性

使用中可能出现的问题:

如果在某些手机中使用 shape 出现黑色填充背景,设置<solid android:color="@color/transparent"/>即可。

Android中shape用于设定形状,可以在selector,layout等里面使用

最常用属性

  • shape 形状,取值有:rectangle矩形(默认),oval椭圆、圆
  • corners  圆角
  • solid 内部填充
  • stroke 边框
 

形状类型 shape

  • 定义shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)

圆角半径 corners

  • android:radius  整型 半径
  • android:topLeftRadius  整型 左上角半径
  • android:topRightRadius  整型 右上角半径
  • android:bottomLeftRadius 整型 左下角半径
  • android:bottomRightRadius 整型 右下角半径

填充颜色 solid,不能与gradient一起使用,他代表固定的颜色

  • android:color 颜色值 填充颜色

描边、边框 stroke

  • android:width 整型 描边的宽度;实线的宽度
  • android:color 颜色值 描边的颜色;实线的颜色
  • android:dashWidth  整型 表示描边的样式是虚线的宽度, 值为0时,表示为实线。值大于0则为虚线;实线的长度
  • android:dashGap  整型 表示描边为虚线时,虚线之间的间隔 即“ - - - - ”;实线与实线之间间隙宽度

渐变色 gradient

  • android:startColor  颜色值 起始颜色
  • android:endColor    颜色值 结束颜色
  • android:centerColor  整型   渐变中间颜色,即开始颜色与结束颜色之间的颜色
  • android:angle   整型  渐变角度  值为45的整数倍。当=0时,渐变色是从左向右,然后逆时针方向转;当=90时,从下往上。默认是 0。该属性只有在type=linear情况下起作用,默认的type为linear。
  • android:type   渐变类型,取值有三个: linear 线性渐变,默认;  radial 放射性渐变,以开始色为中心; sweep 扫描线式的渐变。
  • android:useLevel   ["true" | "false"] 如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。false有渐变色
  • android:gradientRadius 整型 渐变色半径。当 android:type="radial" 时【必须】使用,否则会报错。
  • android:centerX    整型   渐变中心X点坐标的相对位置
  • android:centerY   整型   渐变中心Y点坐标的相对位置

内边距大小 padding

  • android:left  整型 左内边距
  • android:top   整型 上内边距
  • android:right  整型 右内边距
  • android:bottom 整型 下内边距

图形大小 size ,指定图形的宽高,只有控件指定的是wrap_content时,这个属性才会起作用
  • android:width 整型 宽度
  • android:height 整型 高度

下面的属性只有在android:shape="ring时可用:
  • android:innerRadius 尺寸,内环的半径。
  • android:innerRadiusRatio浮点型,以环的宽度比率来表示内环的半径
  • android:thickness尺寸,环的厚度
  • android:thicknessRatio浮点型,以环的宽度比率来表示环的厚度,不完整的圆环
  • android:useLevelboolean值,如果当做是LevelListDrawable使用时值为true,否则为false
 

实例--圆形与矩形


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
 
    <solid android:color="@color/green" />
 
    <stroke
        android:width="1dip"
        android:color="@color/red" />
 
    <padding
        android:bottom="5dp"
        android:left="10dp"
        android:right="10dp"
        android:top="5dp" />
 
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 默认为rectangle,矩形 -->
    <solid android:color="@color/yellow" />
 
    <corners android:radius="0dp" />
 
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
 
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <!-- 默认为rectangle,矩形 -->
    <solid android:color="@color/write" />
 
    <corners android:radius="5dp" />
 
    <stroke
        android:width="1dp"
        android:color="@color/red" />
 
    <padding
        android:bottom="2dp"
        android:left="5dp"
        android:right="5dp"
        android:top="2dp" />
 
</shape>
 

实例--圆形描边


思路:

  • 设置shape形状为圆形oval,边框大小为1dp,填充颜色为透明
  • 为保证整体大小不变,需将图片宽高-2,并设置padding为1;为保证居中,设置margin值为1;
  • 将上述的shape作为背景,在代码中将src裁剪成圆形。

        <ImageView
            android:id="@+id/iv_52"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="1dp"
            android:layout_marginTop="1dp"
            android:background="@drawable/bg_selector"
            android:clickable="true"
            android:padding="1dp"
            android:scaleType="centerCrop"

android:src="@drawable/img_user_icon" />


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:state_pressed="true"><shape android:shape="oval">
            <stroke android:width="1dp" android:color="@color/yellow" />
 
            <solid android:color="@color/translate" />
        </shape></item>
    <item><shape android:shape="oval">
            <stroke android:width="1dp" android:color="@color/red" />
 
            <solid android:color="@color/translate" />
        </shape></item>
 

</selector>

 

实例--环形



        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp" >
 
            <TextView
                android:id="@+id/tv_43"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:background="@drawable/shape_ring3"
                android:gravity="center"
                android:text="圆环" />
 
            <ImageView
                android:id="@+id/iv_44"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerInParent="true"
                android:background="@drawable/shape_oval3"
                android:src="#0000" />

</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="5dp"
    android:shape="ring"
    android:thickness="12dp"
    android:useLevel="false" >
 
    <!-- android:innerRadius 内环的半径。android:thickness环的厚度 -->
    <solid android:color="@color/yellow" />
    <!-- 【内环半径+环的厚度】*2+【描边的宽度】=【12+5】*2+【6】=40,因为40=android:layout_width="40dp" ,所以边缘不会被切割 -->
    <stroke
        android:width="6dp"
        android:color="@color/red" />
 
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="15dp"
    android:shape="ring"
    android:thickness="4.5dp"
    android:useLevel="false" >
 
    <!-- android:innerRadius 内环的半径。android:thickness环的厚度 -->
    <solid android:color="@color/yellow" />
    <!-- 【内环半径+环的厚度】*2+【描边的宽度】=【15+5】*2+【1】=41,因为41>android:layout_width="40dp" ,所以边缘会被切割 -->
    <stroke
        android:width="1dp"
        android:color="@color/red" />
 
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="15dp"
    android:shape="ring"
    android:thickness="4.5dp"
    android:useLevel="false" >
 
    <solid android:color="@color/write" />
 
    <stroke
        android:width="1dp"
        android:color="@color/red" />
 
</shape>
 

附件列表

shape 填充 圆角矩形 圆形 环形的更多相关文章

  1. canva绘制圆角矩形

    在做组态的时候,需要支持矩形圆角格式,但是因为canvas本身不带有圆角矩形,需要自行算出坐标进行绘制 方案一.统一圆角 <!DOCTYPE html> <html> < ...

  2. Android 中shape的使用(圆角矩形)

    一.在res/drawable文件夹下创建一个名为gradient_box的xml文件: <?xml version="1.0" encoding="utf-8&q ...

  3. RoundedImageView,实现圆形、圆角矩形的注意事项

    RoundedImageView是gitHub上面的一个开源组件(https://github.com/vinc3m1/RoundedImageView),实现一些圆形或者圆角矩形是很方便的, < ...

  4. 详解使用CSS3绘制矩形、圆角矩形、圆形、椭圆形、三角形、弧

    1.矩形 绘制矩形应该是最简单的了,直接设置div的宽和高,填充颜色,效果就出来了. 2.圆角矩形 绘制圆角矩形也很简单,在1的基础上,在使用css3的border-radius,即可. 3.圆 根据 ...

  5. canvas文字自动换行、圆角矩形画法、生成图片手机长按保存、方形图片变圆形

    canvas的文字自动换行函数封装 // str:要绘制的字符串 // canvas:canvas对象 // initX:绘制字符串起始x坐标 // initY:绘制字符串起始y坐标 // lineH ...

  6. [BOT] 一种android中实现“圆角矩形”的方法

    内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...

  7. 用贝赛尔曲线把图片, 按钮, label 绘成圆 或圆角矩形

    //创建圆形遮罩,把用户头像变成圆形 /* *CGPointMake(35, 35)  是绘图的中心点,  如果想把控件居中绘圆, 一般用控件的中心点,   radius 是圆半径   startAn ...

  8. Android中实现圆角矩形及半透明效果。

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们为了美观,有时候需要使用圆角矩形,或半透明之类的效果,在网页设计中很容易实现.但在Android开发中 ...

  9. Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形) .

    1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, ...

随机推荐

  1. .NET垃圾回收与内存泄漏

    相信大家一定听过,看过甚至遇到过内存泄漏.在 .NET 平台也一定知道有垃圾回收器,它可以让开发人员不必担心内存的释放问题,因为它会自定管理内存.但是在 .NET 平台下进行编程,绝对不会发生内存泄漏 ...

  2. 安卓环境搭建(1)hellow world

    本系列适合0基础的人员,因为我就是从0开始的,此系列记录我步入Android开发的一些经验分享,望与君共勉!作为Android队伍中的一个新人的我,如果有什么不对的地方,还望不吝赐教. 在开始Andr ...

  3. python 安装PyV8 和 lxml

    近来在玩python爬虫,需要使用PyV8模块和lxml模块.但是执行pip install xx 或者easy_install xx 指令都会提示一些错误.这些错误有些是提示pip版本过低或者缺少v ...

  4. Java文件末尾追加字符串

    Java进行文件输出时,有时候想直接向已有文件末尾追加字符,而不是从头开始写,可以采用以下三种方式实现: package test; import java.io.File; import java. ...

  5. windows10 预览版 中英文官方下载地址+激活密钥+网盘分享

    windows10 预览版 中英文官方下载地址+激活密钥+网盘分享 产品密钥:NKJFK-GPHP7-G8C3J-P6JXR-HQRJR 英语 64 位 (x64)  http://iso.esd.m ...

  6. Node.js模块 加载笔记

    //核心模块就是Node.js标准API种提供的模块,如fs,http,net.vm等.官方提供,编译成二进制代码//核心模块拥有最高的加载优先级 //文件模块则是存储为单独的文件(或文件夹)的模块, ...

  7. 关于如何在C语言中嵌入汇编命令

    转载自:http://www.keil.com/support/docs/2308.htm C51: GETTING INLINE ASSEMBLY TO WORK Information in th ...

  8. 国内大学毕业论文LaTeX模板集合

    国内大学毕业论文LaTeX模板集合 薛瑞尼的清华大学学位论文LaTeX模板http://sourceforge.net/projects/thuthesis/ 北大论文文档 LaTeX 模板 pkut ...

  9. Miller_Rabin素数判断,rho

    safe保险一点5吧.我是MR: ; int gcd(int a,int b){return !b?a:gcd(b,a%b);} int mul(int a,int b,int p){ )*p); ? ...

  10. BZOJ2274: [Usaco2011 Feb]Generic Cow Protests

    2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solve ...