使用xml来绘制简单的图像

前端之家收集整理的这篇文章主要介绍了使用xml来绘制简单的图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

android可以使用xml来绘制一些简单的图形,如渐变色、单色背景等。

1 使用xml绘制的好处

一般如果可以用xnl实现尽量不用图片资源实现。这样做的好处有几点:

1.1 使用灵活,想改就改

使用xml绘制的图片是使用代码实现的。所以要修改个颜色、修改个圆角什么的简单快捷。相反的,图片资源的修改必须找美工一遍遍的改。

1.2 节省资源空间

xml代码占用的空间要比图片小的多的多的多了,这是一个很好的优化android内存资源的方法


2 xml的使用

2.1 使用layer-list实现带阴影的按钮背景

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 阴影 -->
  4. <item>
  5. <shape android:shape="rectangle">
  6. <solid android:color="#33000000" />
  7. </shape>
  8. </item>
  9. <!-- 背景 -->
  10. <item android:bottom="3px">
  11. <shape android:shape="rectangle">
  12. <solid android:color="#FF13A4E0" />
  13. </shape>
  14. </item>
  15. </layer-list>

2.2 selection实现按钮点击效果

下面是使用shapre直接实现的按钮点击效果

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="false">
  4. <shape android:shape="rectangle">
  5. <corners android:radius="3dp" />
  6. <solid android:color="#FFF3676A" />
  7. </shape>
  8. </item>
  9. <item android:state_pressed="true">
  10. <shape android:shape="rectangle">
  11. <corners android:radius="3dp" />
  12. <solid android:color="#FFEB373A" />
  13. </shape>
  14. </item>
  15. </selector>


2.3 shape的使用

下面介绍下shape的各属性具体用法:

填充

设置填充的颜色

间隔

设置四个方向上的间隔

大小

设置大小

圆角

(同时设置五个属性,则Radius属性无效)

android:Radius="20dp" 设置四个角的半径

android:topLeftRadius="20dp" 设置左上角的半径

android:topRightRadius="20dp" 设置右上角的半径

android:bottomLeftRadius="20dp" 设置右下角的半径

android:bottomRightRadius="20dp" 设置左下角的半径

描边

(dashWidth和dashGap两个属性中只要其中一个设置为0dp或者不设置,则边框为实现边框)

android:width="20dp" 设置边边的宽度

android:color="@android:color/black" 设置边边的颜色

android:dashWidth="2dp" 设置虚线长度

android:dashGap="20dp" 设置虚线间隔长度


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <!-- 图片圆角 -->
  5. <corners
  6. android:radius="9dp"
  7. android:topLeftRadius="2dp"
  8. android:topRightRadius="2dp"
  9. android:bottomLeftRadius="2dp"
  10. android:bottomRightRadius="2dp"/>
  11. <!-- 背景渐变 -->
  12. <gradient
  13. android:startColor="@android:color/white"
  14. android:centerColor="@android:color/black"
  15. android:endColor="@android:color/black"
  16. android:useLevel="true"
  17. android:angle="45"
  18. android:type="radial"
  19. android:centerX="0"
  20. android:centerY="0"
  21. android:gradientRadius="90"/>
  22. <!-- 间隔 -->
  23. <padding
  24. android:left="2dp"
  25. android:top="2dp"
  26. android:right="2dp"
  27. android:bottom="2dp"/><!-- 各方向的间隔 -->
  28. <!-- 大小 -->
  29. <size
  30. android:width="50dp"
  31. android:height="50dp"/><!-- 宽度和高度 -->
  32. <!-- 填充 -->
  33. <solid
  34. android:color="@android:color/white"/><!-- 填充的颜色 -->
  35. <!-- 描边 -->
  36. <stroke
  37. android:width="2dp"
  38. android:color="@android:color/black"
  39. android:dashWidth="1dp"
  40. android:dashGap="2dp"/>
  41. </shape>

猜你在找的XML相关文章