android中通过自定义xml实现你需要的shape效果 xml属性配置,
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决。不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的xml文件放在drawable文件夹中,但是 这样drawable中的内容就非常多,就容易使apk变大。
那么除了使用drawable这样的图片外,就是自定义图形shape,Android上支持以下几种属性gradient、stroke、corners、padding、solid等。
- <shape>
- 2. <!-- 实心 -->
- 3. <solid android:color="#ff9d77"/>
- 4. <!-- 渐变 -->
- 5. <gradient
- 6. android:startColor="#ff8c00"
- 7. android:endColor="#FFFFFF"
- 8. android:angle="270" />
- 9. <!-- 描边 -->
- 10. <stroke
- 11. android:width="2dp"
- 12. android:color="#dcdcdc" />
- 13. <!-- 圆角 -->
- 14. <corners
- 15. android:radius="2dp" />
- <! 内边距>
- 16. <padding
- 17. android:left="10dp"
- 18. android:top="10dp"
- 19. android:right="10dp"
- 20. android:bottom="10dp" />
- 21.</shape>
solid:实心,就是填充的意思
android:color指定填充的颜色
gradient:渐变 gradient定义该形状里面为渐变色填充,
gradient节点主要配置起点颜色、终点颜色、中间点的坐标、中间点的颜色、渐变角度(90度为上下渐变,0为左右渐变)
startColor起始颜色,endColor结束颜色android:angle是渐变角度,必须为45的整数倍。
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。
stroke:描边
android:width="2dp" 描边的宽度,android:color
描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。
corners:圆角
corners节点配置四周园角的半径
android:radius 为角的弧度,值越大角越圆。
padding:边框与背景色的内边距
padding节点主要配置上下左右边距, <?xml version="1.0" encoding="utf-8"?><shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" ><!-- android:shape="" 表示是圆角矩形还是椭圆等等 -->
<solid android:color="#FFFFFF"/> <!-- 背景颜色 -->
<!-- padding 表示内部空间距离背景图片内部边距 的距离 -->
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="5dp" />
<stroke android:width="0.5dp" android:color="#FAA330"/> <!-- 图片边框属性 -->
<corners
android:radius="9dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/> <!-- 圆角的程度 -->
<gradient
android:startColor="#FBA330"
android:centerColor="#FBA330"
android:endColor="#FBA330"
android:type="linear"//
android:angle="90"
android:centerX="0.5"
android:centerY="0.5" />
</shape>
android 颜色渐变是指通知xml或者java代码,设置相关参数,是界面的某个指定的视图显示成从开始位置的颜色,逐渐过度到结尾位置的颜色的技术。
android颜色渐变的分类有:
LinearGradient线性渐变
RadialGradient镜像渐变
SweepGradient角度渐变
一、LinearGradient线性渐变
顾名思义,是只颜色在一个直线方向上逐渐改变。
文件代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <gradient android:endColor="#0000FF" android:startColor="#FF0000" android:type="linear" /> </shape>
效果:
二、RadialGradient镜像渐变
镜像渐变就是楼主问的问题了:只要将type设置为oval,然后增加
android:gradientRadius
属性。
楼主特殊要求是圆形的话,需要在shape里面添加
android:shape="oval"
文件代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <gradient android:endColor="#0000FF" android:gradientRadius="100%p" android:startColor="#FF0000" android:type="linear" /> </shape>
效果:
三、 SweepGradient角度渐变
是指以中心点为射线的一个断点,顺时针旋转所扫过的区域,颜色逐渐改变的一种渐变方式
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:endColor="#0000FF" android:startColor="#FF0000" android:type="sweep" /> </shape>
效果图: