Android自定义图形-Shape
1 概述
1.1 编写目的
学习Android自定义图形shape相关的知识,记录下来,方便后续学习及查询。
2 Shape图形
Android上Shape有以下几种属性shape、 corners、 gradient、 padding、size、solid、stroke,其中shape为一级属性,其余为shape子属性,子属性可以选择定义。基本结构如下图:
<?xml version="1.0"encoding="utf-8"?>
<shape ><corners /><gradient /><padding /><size /><solid /><stroke />
</shape>
2.1 Corners属性(圆角)
<cornersandroid:radius="10dp"android:topLeftRadius="10dp"android:topRightRadius="20dp"android:bottomRightRadius="30dp"android:bottomLeftRadius="40dp"/>
共有五个属性,用于设置四个圆角半径。
2.1.1 Radius(圆角)
radius是统一设置四个圆角半径,如下图所示:
2.1.2 TopLeftRadius、TopRightRadius、BottomRightRadius、BottomLeftRadius(左上、右上、左下、右下圆角)
分别设置左上、右上、左下、右下角的圆角半径,和radius同时设置时,会覆盖radius。如下图所示:
2.2 Gradient属性(渐变色)
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><gradientandroid:type="radial"android:angle="0"android:startColor="@color/red"android:centerColor="@color/green"android:endColor="@color/white"android:centerX="0.3"android:centerY="0.5"android:useLevel="false"android:gradientRadius="200dp"/>
</shape>
2.2.1 Type(渐变类型)
Type:取值["linear" | "radial" | "sweep"]共有3中渐变类型,分别是线性渐变(默认)、放射渐变、扫描式渐变。下面分别是三种渐变:
2.2.2 Angle(渐变角度)
仅对线性渐变有效,0为从左到右(逆时针旋转),必须为45的倍数,90为从下到上。下面分别为90和225:
2.2.3 StartColor、CenterColor、EndColor(渐变颜色)
分别是渐变开始、中间、结束颜色。中间色可以不用。如2.2.1和2.2.2图形所示。
2.2.4 CenterX、CenterY(渐变中心相对位置)
渐变中心相对位置,取值[0~1],默认为0.5正中间。如下图所示:
2.2.5 GradientRadius(渐变的半径)
只有当渐变类型为radial时才有用,且radial时必须使用gradientRadius。如下图所示。
2.2.6 UseLevel
UseLevel:通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。
2.3 Padding属性(内边距)
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><paddingandroid:bottom="2dp"android:left="3dp"android:right="3dp"android:top="2dp"/>
</shape>
共有四个属性,分别是下、左、右、上的内边距(内容和边框的距离)。
2.4 Size属性(宽高)
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><sizeandroid:width="180dp"android:height="100dp"/>
</shape>
设置图形宽高,共两个属性,分别是图形的宽度和高度。
2.5 Solid属性(填充)
自身只有一个属性,用来指定填充颜色,没有定义就不填充。
2.5.1 Solid和Gradient(填充和渐变)
填充和渐变同时使用时,定义在后面的生效,前面的被覆盖。如下图所示:
2.6 Stroke属性(描边)
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><strokeandroid:width="5dp"android:color="@color/red"android:dashWidth="100dp"android:dashGap="50dp"/>
</shape>
这是描边属性,可以定义描边的宽度,颜色,虚实线等。
2.6.1 Width、Color(宽度、颜色)
Width:描边的宽度,Color:描边的颜色。
2.6.2 DashWidth、DashGap(虚线宽度、虚线间隔)
DashWidth:虚线的宽度(一段虚线的长度),DashGap:虚线的间隔(不定义或者为0时,不显示虚线)。
2.7 Shape属性(形状)
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:dither="false"android:tint="@color/red"android:tintMode="src_atop"android:visible="true"android:innerRadius="100dp"android:innerRadiusRatio="20"android:thickness="100dp"android:thicknessRatio="6"android:useLevel="false">
</shape>
其中shape、tint、tintMode、dither、visible、useLevel等属性是共有的,剩下都是shape等于ring时所特有的。
Tint:Shape着色。
TintMode:着色模式。
Dither:是否使用抖动技术。
Visible:可见与不可见。
UseLevel:如果当做是LevelListDrawable使用时值为true,否则为false。
InnerRadius:内环的半径。
InnerRadiusRatio:以环的宽度比率来表示内环的半径。
Thickness:环的厚度ThicknessRatio:以环的宽度比率来表示环的厚度。
2.7.1 Shape(图形形状)
Shape:取值["rectangle" | "oval" | "line" | "ring"]共有4中形状,分别是矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)。默认为矩形,下面分别是四种形状:
2.7.2 Tint、TintMode(着色和着色模式)
使用Tint后会覆盖solid属性,solid无效。TintMode(使用比较少,下面有不同模式显示状态,先画圆再画矩形框,设置蓝色矩形框的TintMode属性)。如下图所示:
着色模式参考:Android图形 - Bitmap(2.13)
2.7.3 Dither(抖动技术)
是否启用抖动技术。值为false和true,默认true。(是一种图像算法,在不同色域的显示屏上修正颜色)。
2.7.4 Visible(可见性)
Visible控制图形是否可见。
2.7.5 UseLevel(LevelListDrawable)
如果当做是LevelListDrawable使用时值为true,否则为false;Shape取值"ring"(圆环)时UseLevel必须等于false,等于True无法显示。
2.7.6 InnerRadius、InnerRadiusRatio(内环半径)
InnerRadiusRatio在不定义InnerRadius时有效,内环半径=环的宽度/ InnerRadiusRatio。如下图所示:
2.7.7 Thickness、ThicknessRatio(环的厚度)
ThicknessRatio在不定义Thickness时有效,环的厚度=环的宽度/ ThicknessRatio。如下图所示:
Android自定义图形-Shape相关推荐
- android 自定义图形,Android自定义View之图形图像(模仿360的刷新球自定
概述: 360安全卫士的那个刷新球(姑且叫它刷新球,因为真的不知道叫什么好,不是dota里的刷新球!!),里面像住了水一样,生动可爱,看似简单,写起来不太简单,本例程只是实现了它的部分功能而已,说实话 ...
- android 画布叠加,Android自定义图形,图形的拼接、叠加、相容
直接上Xfermode子类: AvoidXfermode 指定了一个颜色和容差,强制Paint避免在它上面绘图(或者只在它上面绘图). PixelXorXfermode 当覆盖已有的颜色时,应用一 ...
- Android中使用shape自定义纯色图片总结
android中通过自定义xml实现你需要的shape效果 xml属性配置, 在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如 ...
- Android自定义Shape
1.圆角控件 首先,定义形状: drawable/roundctrl.xml <?xml version="1.0" encoding="UTF-8"?& ...
- Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
需要图片和源码点赞关注收藏后评论区留言~~~ 一.位图与图形互转 Drawable用于在界面上展示图片,Bitmap用于加工图像数据,所以两者之间的转换非常有必要,位图图形BitmapDrawable ...
- Android开发之shape自定义ProgressBar进度条样式
1.xml代码方式一:如下(此shape是放到drawable目录下的) <rotate xmlns:android="http://schemas.android.com/apk/r ...
- Android 自定义shape圆形按钮
Shape的属性: solid 描述:内部填充 属性:android:color 填充颜色 size 描述:大小 属性: android:width 宽 android:height 高 gradie ...
- Android:自定义Shape 加上阴影shadow之方法
直接用layer-list来实现,在项目 res->drawable中创建一个xml,如果列表中有layer-list选择的话直接选择创建,如果没有的话就随意(似乎4.0以下没有 layer-l ...
- Android 自定义环形圆形显示统计数据z
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
最新文章
- Python爬虫之reuqests实现简单网页采集--网页采集教程
- 【自然框架】之“元数据”的威力
- java的排序_java排序
- matlab播放 视频帧,如何把连续视频帧转为视频的matlab代码 | 学步园
- 盘点Spring Boot最核心的27个注解
- Python:通过SNMP协议获取H3C、华为交换机的VLAN信息及ARP地址表
- vue 组件 - 非单文件组件
- poj 2255 Tree Recovery
- python RE表达式规则剩余规则
- session同步问题
- atmega328 48手动解锁 擦除
- ViewState EnableViewState 禁用与使用心得
- database2sharp mysql_Database2Sharp免费版
- 《孽海记·思凡》唱段·风吹荷叶煞
- 设计模式 装饰者模式 带你重回传奇世界
- 水泥行业超低排放政策频发,企业如何完成超低排放改造?
- Ble低功耗蓝牙和蓝牙mesh网络之间的关系
- sql monitor简介
- linkerd mysql_《Linkerd官方文档》在本地运行Linkerd
- 学员洒泪分享,中富金石投顾骗局?血本无归?