Android绘图基础之 ~~~ 在XML中绘图

来源:互联网 发布:企业的域名 编辑:程序博客网 时间:2024/05/16 12:07

XML不仅能能实现布局,同样也可以绘制图形。其中有几个常用的API我们来介绍一下;

 Shape , Layer , Selector 

1. Shape : 通过Shape我们可以绘制各种形状,下面我们来介绍具体属性

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 默认是rectangle--><!--android:shape=["rectangle"|"ring"|"line"|"oval"]>-->    <!-- 当shape是rectangle时使用  半径会被后面的单个半径给覆盖-->    <corners        android:bottomLeftRadius="1dp"        android:bottomRightRadius="1dp"        android:radius="1dp"        android:topLeftRadius="1dp"        android:topRightRadius="1dp" />    <!--设置渐变 -->    <gradient        android:angle="90"        android:centerColor="@color/colorPrimaryDark"        android:centerX="0"        android:centerY="0"        android:endColor="@color/white"        android:gradientRadius="10dp"        android:startColor="@color/colorAccent"        android:type="linear" />    <!-- 设置填充颜色-->    <solid android:color="@color/white" />    <!-- 设置padding-->    <padding        android:bottom="5dp"        android:left="5dp"        android:right="5dp"        android:top="5dp" />    <!-- 设置边框 宽度 颜色  dashWidth表示是虚线宽度 dashGap 表示为虚线间隔-->    <stroke        android:width="2dp"        android:color="@color/colorAccent"        android:dashGap="4dp"        android:dashWidth="1dp" /></shape>

 2.Layer : 类似图层的概念,各个视图相互叠加在一起 例如:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 图片一  -->    <item android:drawable="@drawable/ic_launcher" /><!--图片二 top 等属性是向里缩进的大小 -->    <item android:drawable="@drawable/ic_launcher"        android:top="10dp"        android:bottom="10dp"        android:right="5dp"        android:left="5dp"/><!-- 图层是有先后顺序的 后面的覆盖前面的 --></layer-list>


3.Selector : 是选择器 .根据不同的状态来做出不同的反馈。

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!--当按压时加载这一条-->    <item android:drawable="@drawable/ic_launcher" android:state_pressed="true" />    <!--获取焦点时加载这一条-->    <item android:drawable="@drawable/ic_launcher" android:state_focused="true" /><!--当按下且获取焦点时,加载-->    <item android:drawable="@drawable/ic_launcher"        android:state_focused="true" android:state_pressed="true" /><!--选中时,加载-->    <item android:state_checked="true" android:drawable="@drawable/ic_launcher"/>    <!--当没有设置状态时,就是使用默认状态(也就是什么状态都能匹配)-->    <!-- 这条item不放在第一个 ,因为他放在第一个,其他都将不会加载!!-->    <item android:drawable="@drawable/gradient" />    <!-- 还有其他一些属性,大致相同--></selector>




0 0