安卓xml绘图

来源:互联网 发布:网络暴力乔任梁 编辑:程序博客网 时间:2024/06/05 08:58

xml在安卓中除了作为布局文件和配置文件外,还能辅助我们绘图。下面我们来看一下xml在这方面的应用。

1.Bitmap

使用Bitmap引用图片,可直接将图片转成Bitmap在程序中使用。

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"     android:src="@drawable/ic_launcher">    </bitmap>

2.Shape

使用shape可在xml中绘制各种形状。以下列出了shape的所有用途。

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape=["rectangle"|"oval"|"line"|"ring"]>     <!-- 使用shape为rectangle时配置corner -->    <corners                android:radius="integer"        android:topLeftRadius="integer"        android:topRightRadius="integer"        android:bottomLeftRadius="integer"        android:bottomRightRadius="integer"        />    <!-- 图形渐变 -->    <gradient         android:angle="integer"        android:centerX="integer"        android:centerY="integer"        android:centerColor="color"        android:endColor="color"        android:startColor="color"        android:gradientRadius="integer"        android:useLevel=["ture"|"false"]        android:type=["linear"|"radial"|"sweep"]                     />    <!-- 内边距 -->    <padding        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer"        />    <!-- 指定大小,一般用在imageView配合scaleType属性使用 -->    <size         android:width="integer"        android:height="integer"        />    <!-- 填充色 -->    <solid         android:color="color"/>    <!-- 指定边框 -->    <stroke        android:width="integer"        android:color="color"        android:dashWidth="虚线宽度integer"        android:dashGap="虚线间隔宽度integer"/>          </shape>
3.Layer

Layer类似ps中的图层的概念,可实现图像叠加的效果。
用法如下:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >   <!-- 图片1 -->   <item       android:drawable="@drawable/ic_launcher"/>   <!-- 图片 -->   <item        android:drawable="@drawable/ic_launcher"       android:left="10dip"       android:top="10dip"       android:right="10dip"       android:bottom="10dip"              /></layer-list>


4.Selector

selector用于实现静态绘图中的事件反馈。例如点击获得焦点时图片反白效果。也可通过点击事件设置不同的图片。


<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 默认时的图片背景 -->    <item android:drawable="@drawable/bitmap" />    <!-- 没有焦点时的图片背景 -->    <item android:state_focused="false"        android:drawable="@drawable/ic_launcher"/>    <!-- 非触摸模式下获得焦点并单击时的背景图片 -->        <item android:state_focused="false"        android:state_pressed="true"        android:drawable="@drawable/ic_launcher"/>    <!-- 触摸模式下单击时的背景图片 -->    <item android:state_focused="false"        android:state_pressed="true"        android:drawable="@drawable/ic_launcher"/>    <!-- 选中时的背景图片 -->    <item  android:state_selected="true"        android:drawable="@drawable/ic_launcher"/>    <!-- 获得焦点时的背景图片 -->    <item android:state_focused="true"        android:drawable="@drawable/ic_launcher"/>        </selector>



1 0
原创粉丝点击