属性

使用中可能出现的问题:

如果在某些手机中使用 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. Build Android-x86 ICS 4 Virtualbox from Google Virtualbox Target and Intel Kernel 编译体验

    最近一直在研究android源码的编译,应该说研究的很辛苦,最难的是下源码,总是不停的断掉,最后感谢公司的高网速,找到方法后12G的源码只花了1个小时就下完了. 参考以下网址:http://softw ...

  2. linux dd命令测试U盘读写速度

    1. dd命令简述: if=输入文件, of=输出文件, ibs=一次读取字节数, obs=一次写入字节数, bs=设置一次读取写入的字节数, skip=跳过的bs数, count=拷贝的块数 2. ...

  3. WPS 去掉自动打开的文档漫游和在线模板

    关闭文档漫游  在cmd(命令提示符)中输入regedit.exe回车,将弹出”注册表编辑器“,选择HKEY_CURRENT_USER>>Software>>Kingsoft& ...

  4. 请给出异步加载js方案

    请给出异步加载js方案,不少于两种 默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很 ...

  5. 移动H5前端性能优化指南[托尼托尼研究所]

    概述 1. PC优化手段在Mobile侧同样适用2. 在Mobile侧我们提出三秒种渲染完成首屏指标3. 基于第二点,首屏加载3秒完成或使用Loading4. 基于联通3G网络平均338KB/s(2. ...

  6. jquery 事件绑定(1)

    $(function(){ $("#panel h5.head").bind("click",function(){ $(this).next().show() ...

  7. 一、UITableView的属性

    一.UITableView的属性 NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多.不必再去建全局变量sectio ...

  8. Python三元表达式

    我们知道Python没有三元表达式,但是我们通过技巧达到三元表达式的效果. 摘自<Dive Into Python>: 在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的 ...

  9. UPdate 延时盲注之小技巧

    Title:UPdate 延时盲注之小技巧  --2014-06-05 15:21 UPDATE TABLEZZZ SET zz=111111 where id=$id 当TABLEZZZ表为空的时候 ...

  10. 【转】Android源代码查看途径

    原文网址:http://www.it165.net/pro/html/201501/32967.html 作为一个android coder,多阅读android源码对提高android开发水平是很有 ...