公司为什么要网站备案,阿坝网站设计,网站开发前端兼职,安徽省建设工程造价信息网官网设置背景时#xff0c;经常这样 android:background“drawable/xxx” 。如果是纯色图片#xff0c;可以考虑用 shape 替代。
shape 相比图片#xff0c;减少资源占用#xff0c;缩减APK体积。
开始使用。
?xml version1.0 encodingutf-8?…设置背景时经常这样 android:background“drawable/xxx” 。如果是纯色图片可以考虑用 shape 替代。
shape 相比图片减少资源占用缩减APK体积。
开始使用。
?xml version1.0 encodingutf-8?
shapexmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shape[rectangle | oval | line | ring] cornersandroid:radiusintegerandroid:topLeftRadiusintegerandroid:topRightRadiusintegerandroid:bottomLeftRadiusintegerandroid:bottomRightRadiusinteger /gradientandroid:angleintegerandroid:centerXintegerandroid:centerYintegerandroid:centerColorintegerandroid:endColorcolorandroid:gradientRadiusintegerandroid:startColorcolorandroid:type[linear | radial | sweep]android:useLevel[true | false] /paddingandroid:leftintegerandroid:topintegerandroid:rightintegerandroid:bottominteger /sizeandroid:widthintegerandroid:heightinteger /solidandroid:colorcolor /strokeandroid:widthintegerandroid:colorcolorandroid:dashWidthintegerandroid:dashGapinteger /
/shape概览
使用 shape 可以实现 矩形、椭圆、直线、圆环。
corners 设置圆角android:radius 设置统一的圆角。也可以单独设置四个角的圆角。gradient 渐变有线性渐变默认、放射渐变类似中心往外扩散的效果、扫描式渐变转一圈的效果。padding 内边距。和 View 的 padding 使用一样。可以不设置由 View 来决定。size 大小。设置宽高和 View 的 padding 使用一样。可以不设置由 View 来决定。solid 填充颜色。stroke 描边。可以设置边界的颜色设置边界边缘为虚线。
使用方法
1.在 res/drawable/ 目录创建 shape_demo.xml 。2.在布局文件中 android:background“drawable/shape_demo”
矩形
默认直角矩形
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shaperectangle!--矩形--!--填充颜色--solid android:colorcolor/purple_200 /
/shape圆角
用 corners 设置圆角圆角的幅度由 android:radius 控制。
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shaperectangle!--矩形--!--填充颜色--solid android:colorcolor/purple_200 /!--圆角--cornersandroid:radius10dp/
/shape描边
用 stroke 描边默认边缘时曲线添加了 android:dashWidth 、android:dashGap 就是虚线。
android:width 指定宽度android:color 是边缘颜色android:dashWidth 是虚线线段的宽度android:dashGap 是虚线之间的间隔
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shaperectangle!--矩形--!--填充颜色--solid android:colorcolor/purple_200 /!--圆角--cornersandroid:radius20dp/!--描边--strokeandroid:width2dpandroid:colorcolor/my_redandroid:dashWidth10dpandroid:dashGap2dp /
/shape渐变色
用 gradient 设置渐变色
android:type 渐变色类型线性渐变默认、放射渐变类似中心往外扩散的效果、扫描式渐变转一圈的效果。android:angle 渐变开始角度。线性渐变下有效。 Angle of the gradient, used only with linear gradient. Must be a multiple of 45 in the range [0, 315].android:startColor 渐变开始的颜色android:centerColor 渐变中间的颜色android:endColor 渐变结束的颜色
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shaperectangle!--矩形--!--圆角--corners android:radius20dp /gradientandroid:centerColorandroid:color/holo_orange_darkandroid:endColorcolor/my_redandroid:startColorandroid:color/holo_green_dark //shape几种矩形效果对比
学会了矩形其他的也就会了。
椭圆
线性渐变
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shapeoval!--椭圆--!--填充颜色--solid android:colorcolor/teal_200 /!--描边--strokeandroid:width2dpandroid:colorcolor/purple_200 /gradientandroid:endColorcolor/teal_200android:startColorcolor/my_redandroid:typelinear //shape放射渐变在 线性渐变 基础上把 android:type 改为 radial 同时设置 android:gradientRadius 它决定内圆的大小。
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shapeoval!--椭圆--!--填充颜色--solid android:colorcolor/teal_200 /!--描边--strokeandroid:width2dpandroid:colorcolor/purple_200 /gradientandroid:gradientRadius50dpandroid:endColorcolor/teal_200android:startColorcolor/my_redandroid:typeradial /
/shape扫描式渐变在 线性渐变 基础上把 android:type 改为 sweep 。
linear 、radial 、sweep 三种渐变色的对比
直线/虚线
直线
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shapeline!--线--size android:width100dp android:height2dp/!--直线--strokeandroid:width2dpandroid:colorcolor/my_red//shape虚线虚线就是直线加上描边效果。
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shapeline!--线--size android:width100dp android:height2dp/!--虚线--strokeandroid:width2dpandroid:colorcolor/teal_200android:dashWidth2dpandroid:dashGap2dp //shape对比
圆环
android:shape“ring”
android:innerRadius 内圆半径直接设置 dp 值。android:thickness 圆环厚度直接设置 dp 值。android:useLevel 设为 false 否则不显示。android:innerRadiusRatio 内圆半径圆环宽度占比的形式如 设为 4 意思是 内圆半径 圆环宽度 / 4 。android:thicknessRatio 圆环厚度圆环宽度占比的形式如 设为 4 意思是 内圆半径 圆环宽度 / 4 。
本例 View 限定宽高都为 100 dp 这两种写法圆环大小是一样的。 写法1
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:shaperingandroid:innerRadius25dpandroid:thickness25dpandroid:useLevelfalse!--颜色--solid android:colorcolor/purple_200/
/shape写法2
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:innerRadiusRatio4android:shaperingandroid:thicknessRatio4android:useLevelfalse!--颜色--solid android:colorcolor/my_red /!--渐变色--gradientandroid:angle0android:endColorcolor/teal_200android:startColorcolor/my_red/
/shape写法 2 , gradient 中设置 android:angle“90” 看下和 0 的对比
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:innerRadiusRatio4android:shaperingandroid:thicknessRatio4android:useLevelfalse!--颜色--solid android:colorcolor/my_red /!--渐变色--gradientandroid:angle90android:endColorcolor/teal_200android:startColorcolor/my_red/
/shape三种效果对比
android:angle 为 0 是中间的效果左边是开始渐变的颜色右边是结束渐变的颜色。android:angle 为 90 是右边的效果下面是开始渐变的颜色上面是结束渐变的颜色。