Android中资源总结之Drawable

来源:互联网 发布:bgp是什么网络类型 编辑:程序博客网 时间:2024/04/24 17:46

图片资源

位置:(\res\drawable)

这个就不说了,都一直用的。.png 、 .jpg、.9.png

StateListDrawable

位置:(\res\drawable)

代码示例

<?xml version="1.0" encoding="utf-8" ?>     <selector xmlns:android="http://schemas.android.com/apk/res/android">     <!-- 触摸时并且当前窗口处于交互状态 -->      <item android:state_pressed="true" android:state_window_focused="true" android:drawable= "@drawable/pic1" />    <!--  触摸时并且没有获得焦点状态 -->      <item android:state_pressed="true" android:state_focused="false" android:drawable="@drawable/pic2" />      <!--选中时的图片背景-->      <item android:state_selected="true" android:drawable="@drawable/pic3" />       <!--获得焦点时的图片背景-->      <item android:state_focused="true" android:drawable="@drawable/pic4" />      <!-- 窗口没有处于交互时的背景图片 -->      <item android:drawable="@drawable/pic5" />   </selector> 

LayerDrawable

位置:(\res\drawable)

一个LayerDrawable是一个可以管理一组drawable对象的drawable。在LayerDrawable的drawable资源按照列表的顺序绘制,列表的最后一个drawable绘制在最上层。
它所包含的一组drawable资源用多个元素表示,一个元素代表一个drawable资源。

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/black_lotus"        android:left="20dp"        android:top="20dp">    </item>    <item android:drawable="@drawable/black_lotus"        android:left="40dp"        android:top="40dp">    </item>    <item android:drawable="@drawable/black_lotus"        android:left="60dp"        android:top="60dp">    </item></layer-list>

ShapeDrawable

位置:(\res\drawable)

<shape      xmlns:android="http://schemas.android.com/apk/res/android"      android:shape=["rectangle" | "oval" | "line" | "ring"] >      <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="integer"          android:endColor="color"          android:gradientRadius="integer"          android:startColor="color"          android:type=["linear" | "radial" | "sweep"]          android:usesLevel=["true" | "false"] />      <padding          android:left="integer"          android:top="integer"          android:right="integer"          android:bottom="integer" />      <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>  

ClipDrawable

位置:(\res\drawable)

ClipDrawable代表从其它位图上截取一个”图片片段”,XML中的根元素为clip…/>,截取的方向由clipOrientation控制

<?xml version="1.0" encoding="utf-8"?>  <clip xmlns:android="http://schemas.android.com/apk/res/android"       android:drawable="@drawable/fengjing"      android:clipOrientation="horizontal"      android:gravity="center">  </clip>

ClipDrawable使用参考范例

AnimationDrawable

位置:(\res\anim)

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="5000"android:interpolator="@android:anim/anticipate_overshoot_interpolator"<!--插补器,可以指定动画的变化速度,Android系统的R.anim包含了大量常量,定义了不同的动画速度。>linear_interpolator:匀速变换>accelerate_interpolator:加速变换>decelerate_interpolator:减速变换>-->>    android:shareInterpolator=["true"|"false"] >    <alpha        android:fromAlpha="float"        android:toAlpha="float" />    <scale        android:fromXScale="float"        android:fromYScale="float"        <!-- 中心点 -->         android:pivotX="float"        android:pivotY="float"        android:toXScale="float"        android:toYScale="float" />    <rotate        android:fromDegrees="float"        android:pivotX="float"        android:pivotY="float"        android:toDegrees="float" />   <transla        android:fromXDelta="float"        android:toXDelta="float"         android:fromYDelta="float"        android:toYDelta="float"/></set>

简单使用:
代码中定义出AnimationDrawable对象,并设置到view的background上,然后设置开始播放就可以了。

AnimationDrawable ad = (AnimationDrawable) getResources().getDrawable( R.drawable.bootanimation);mView.setBackgroundDrawable(ad);ad.start();
1 0
原创粉丝点击