Android Drawable系列(1):自定义背景以及注意事项
来源:互联网 发布:淘宝网佛经早晚课读本 编辑:程序博客网 时间:2024/05/17 08:10
0、 Shape自身属性
- android:shape=[“rectangle” | “oval” | “line” | “ring”] //指定shape的形状,矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)
- android:tint=”#FFFFFFFF” //将图片渲染成指定颜色,无论solid中定义的是什么颜色,可以用于改变图标的颜色
- android:dither=true //图像的抖动处理,当每个颜色值以低于8位表示时,对应图像做抖动处理可以实现在可显示颜色总数比较低(比如256色)时还保持较好的显示效果
- android:tintMode //API 21+未做详细研究
- android:innerRadius //内环的半径 以下属性均为shape=ring 时有效使用
- android:innerRadiusRatio //浮点型,以环的宽度比率来表示内环的半径,例如,如果android:innerRadiusRatio=”5”,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.
- android:thickness //环的厚度
- android:thicknessRatio //浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio=”2”,那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.
1、 padding
- android:top、bottom、left、right //上下左右,很简单就是显示在shape上的内容与shape边界的距离
2、 size
- android:width、 android:height
- 用于矩形时,设置矩形的宽高;
- 用于椭圆时,设置椭圆外切矩形的宽高,当宽与高相等时,为正圆,否则为椭圆;
- 用于直线时,宽代表了直线的长度,高没有效果
- 用于圆环时,宽会影响内圆半径和圆环的宽窄,高没有效果。
注意:当我们将一个shape drawable作为一个View组件的背景或是内容时,通常这个drawable都会被缩放。所以实际显示的时候可能与这里的size设置不相符。
3、 solid
- android:color // 实心之意,用这种颜色来填充该shape内部,如果不设置该shape的边框,就能得到一张纯色的drawable。
4、 stroke
- 用笔画出来的轨迹,笔画”的意思,这里指shape的边框。
- 用于矩形和椭圆时,设置其外边界
- 用于直线时,呃,直线就是用这个节点来设置的。
- 用于圆环时,会同时影响内圆和外圆。
- android:width//边框线条的宽度,特别地,用于直线时设置直线的粗细。
- android:color//设置线条的颜色
- android:dashWidth//设置虚线效果里实点的长度
- android:dashGap//设置虚线效果里空白点的长度
5、 corners
- 只用于android:shape=”rectangle”,用于设置矩形的4个角的圆角半径,生成圆角矩形。
- android:radius——同时设置矩形4个角的圆角半径
- android:topLeftRadius——左上角圆角半径
- android:topRightRadius——右上角圆角半径
- android:bottomLeftRadius——左下角圆角半径
- android:bottomRightRadius——右下角圆角半径
注意 - 这四个单独的设置的值会覆盖android:radius设置的值。
<corners android:bottomLeftRadius="15dp" android:radius="5dp" android:topLeftRadius="15dp" />
- 兼容性问题 使用以下代码
<corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp" />
Android2.3.7模拟器下
Android4.4.2模拟器下
解决方案
在res目录下新建一个名为drawable-v12的文件夹,将适配Android3.1+的资源文件(正确的左右方向)放在此文件夹中即可
6、 gradient
- 与solid一样均为shape填充的一种方式,优先级取决于谁被写在后面
- android:startColor、endColor 两者至少得有一个,另一个不指定时默认为白色
- android:type 渐变类型 linear(线性渐变),radial(径向渐变)与android:gradientRadius(用于指定渐变区域圆的半径)属性一同使用,sweep(扫描渐变,顺时针方向渐变,起点为3点钟方向)
- android:angle 指定线性渐变的方向 必须为45的倍数,即只能指定八个方向
- android:centerX、android:centerY 径向渐变与扫描渐变时指定中心点位置
7、 典型举例
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:tint="#FF2A5178"> <solid android:color="#FF009068" /></shape>
参考文章
Shape Drawable的学习
Android中shape的使用
android圆角View实现及不同版本这间的兼容
How can I work around Android issue 9161, where bottomRightRadius and bottomLeftRadius are swapped?
0 0
- Android Drawable系列(1):自定义背景以及注意事项
- Android 自定义背景系列
- Android自定义控件背景及其Drawable以实现扁平化
- Android利用drawable文件夹自定义控件背景、样式
- Android利用drawable文件夹自定义控件背景、样式
- Android开发系列(十八):自定义控件样式在drawable文件夹下的XML实现
- 【Android】自定义控件系列之TextView设置Drawable的大小
- Android Drawable系列——Animation Drawable(动画实现)
- Android drawable 玩转自定义图片以及bug的解决
- Android drawable 玩转自定义图片以及bug的解决
- Android开发笔记(九十五)自定义Drawable
- android 自定义View drawable
- Android自定义drawable
- android自定义drawable图形
- 自定义drawable android:shape
- Android--自定义Drawable基础
- Android自定义Drawable
- android自定义圆形drawable
- .NET程序的性能要领和优化建议
- 自定义Adapter的listview(很重要)
- a different object with the same identifier value was already associated with the session:
- java常用API知识点总结
- 解决maven项目update project更改项目编码的问题
- Android Drawable系列(1):自定义背景以及注意事项
- Codeforces 574B Bear and Three Musketeers
- TestLink基本流程
- 创建安卓桌面快捷图标
- HDU——2010水仙花数
- io知识点总结第一篇
- linux-samba服务搭建
- 关于项目架构资料整理
- HTML文件中文打开乱码的问题