Drawable资源
1.StateListDrawable资源
StateListDrawable对象多显示的Drawable对象会随着目标组件状态的改变而自动切换。
1)根元素为<selector/>
2)包含多个<item/>元素,该元素可以指定如下属性。
android:color或者android:drawable:指定颜色或者drawable对象。
android:state_XXX=true|false 指定一个特定状态。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/item_selector"/> <item android:state_focused="true" android:drawable="@drawable/item_selector"/></selector>
2.LayoutDrawable资源
LayoutDrawable包含一个Drawable数组,因此系统将会按Drawable对象的数组顺序来绘制它们,索引最大的Drawable对象将会被绘制在最上面。
1)根元素为<layer-list/>
2)子元素为<item/>,包含如下属性
android:drawabe 指定drawable对象
android:id 指定一个ID
android:bottom|top|left|right 指定绘制时的位置
<layer-list> <item android:od="@+id/id1" android:drawable="@drawable/ok"/> <item android:id="@+id/id2" android:drawable="@drawable/cancel"/></layer-list>
3)ShapeDrawable资源
ShapeDrawable用于定义一个基本的几何图形.
1)根元素为<shape/>
2)该元素可以指定一下属性android:shape=["rectangle"|"oval"|"line"|"ring"]
<shape> <!--定义四个角的弧度--><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;useLevel=[true|false]/><!--定义内边距--> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer"/><!--定义几何形状的大小--><size android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer"/><!--定义使用单种颜色填充--> <solid android:color="color"/><!--定义绘制边框--><stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer"/> </shape>
4)ClipDrawable资源
clipDrawable资源代表从其他位图上截取的一个图片片段,在XML文件中定义ClipDrawable对象使用<clip/>元素。
clip元素有如下三个属性:
1)android:drawable:指定截取源的Drawable对象。
2)android:clipOrientation:指定截取方向,可设置水平截取或者垂直截取。
3)android:gravity:指定截取时的对齐方式。
使用ClipDrawable对象时可使用setLevel()方法来设置截取的区域大小,当level为0的时候,截取的片段为空;当level为10000的时候,截取整张图片。
编写一个缓慢打开图片的demo:
myclip.xml:
<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:drawable="@drawable/start" android:gravity="center" ></clip>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main" > <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:src="@drawable/myclip" /></RelativeLayout>
package com.app.main;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.graphics.drawable.ClipDrawable;import android.view.Menu;import android.widget.ImageView;public class Main extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ImageView imageView = (ImageView) this.findViewById(R.id.imageview);final ClipDrawable clip = (ClipDrawable) imageView.getDrawable();final Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 0x001) {clip.setLevel(clip.getLevel()+400);}}};final Timer timer=new Timer();timer.schedule(new TimerTask(){@Overridepublic void run() {Message msg=new Message();msg.what=0x001;handler.sendMessage(msg);if(clip.getLevel()>10000){timer.cancel();}}}, 0,100);}}
更多相关文章
- 类和 Json对象
- android 一直在最前面的浮动窗口效果
- Android(安卓)ButterKnife10.0官方接入的坑
- Android(安卓)Adapter详解
- Android应用程序组件Content Provider在应用程序之间共享数据的
- Android的UI两大基石
- android 一直在最前面的浮动窗口效果
- Android(安卓)Json解析工具类
- 关于Android(安卓)studio的build.gradle的内容