属性

使用中可能出现的问题:

如果在某些手机中使用 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. Q我音乐

  2. openMPI小集群安装

    经过一天的努力,终于完成了openMPI的多节点安装,即小集群安装.本文使用的是openmpi-1.6.5,下载地址见:http://www.open-mpi.org/software/ompi/v1 ...

  3. 利用C++ RAII技术自动回收堆内存

    在C++的编程过程中,我们经常需要申请一块动态内存,然后当用完以后将其释放.通常而言,我们的代码是这样的: 1: void func() 2: { 3: //allocate a dynamic me ...

  4. 10分钟 教你学会Linux/Unix下的vi文本编辑器

    10分钟 教你学会Linux/Unix下的vi文本编辑器 vi编辑器是Unix/Linux系统管理员必须学会使用的编辑器.看了不少关于vi的资料,终于得到这个总结.不敢独享,和你们共享. 首先,记住v ...

  5. 实验五:分析system_call中断处理过程

    原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...

  6. spinner 下拉框控件

    spinnerMode=dropdown时,为下拉模式spinnerMode=dialog时,会在界面中间弹出Android:popupBackground=”#f0000000”,可以去除spinn ...

  7. codevs4373 窗口

    题目描述 Description 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: Window position Min val ...

  8. 启动Activity但不显示其界面

    最近在工作中做了一个很简单的任务,制作一个apk,点击该app链接到某一个网站.     代码很简单,只有如寥寥几行: (browser.java) package com.test.browser; ...

  9. windows下和linux下 Redis 安装

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  10. redis 扩展安装使用

    Redis的php客户端库非常之多, Redis推荐客户端链接是:http://redis.io/clients 推荐用phpredis,下载地址:https://github.com/nicolas ...