- 图片资源
图片资源最常见,此处不多说。。。
2. StateListDrawable资源
StateListDrawable用于组织多个Drawable对象,当使用StateListDrawable作为目标组件的背景、前景图片时,StateListDrawable对象所显示的Drawable对象会随目标组件的状态的改变而自动切换。常用于用按钮,点在按钮和没点时按钮是不同的颜色。。。定义StateListDrawable对象的XML文件的 根元素为<selector.../> 子元素<item.../>一般StateListDrawable支持如下状态:例子:<p><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android" data-ke-src="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"></p><p> <item android:state_pressed="true" android:drawable="@drawable/checked"/> <item android:state_pressed="false" android:drawable="@drawable/unchecked"/></p><p></selector></p>
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background" android:drawable="@drawable/grow"/> <item android:id="@android:id/progress" android:drawable="@drawable/ok"/> </layer-list>
<span style="font-family:微软雅黑;background-color:#ffffff;"></span><p><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android" data-ke-src="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ></p><p> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/state" /></p><p> <SeekBar style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/muy_bar" /></p><p></LinearLayout></p>
在Activity没做什么操作,就加载了一下main.xml,结果如下所示,进度条的外观变美咯。。。。3. LayerDrawable资源LayerDrawble和StateListDrawble有点类似,也包含了一个Drawble数组。系统是根据这些Drawble数组的顺序来绘制它们的,索引最大的Drawble对象将会被绘制到最上面。
定义LayerDrawble的xml文件的根元素是<layer-list.../>同样子元素是<item.../>。
其子元素具有如下几个属性:android:drawble:指定作为LayerDrawble元素之一的Drawble对象。
android:id为该Drawble对象的标识
android:buttomltoplleftlbutton它们用于指定一个长度值,用于指定将该Drawble对象绘制到目标组件的指定位置。
4. ShapeDrawable资源
用于定义一个基本的几何图形(矩形、圆形、线条)
定义格式如下:根元素是<shape.../>无子元素,只需定义他的几个属性
-
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 设置填充颜色 --> <solid android:color="#fff"/> <!-- 设置四周的内边距 --> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <!-- 设置边框 --> <stroke android:width="3dip" android:color="#ff0" /> </shape>
-
ClipDrawable资源 ClipDrawable用于从其他位图上截取一个"图片片段"
根元素是<clip.../>无子元素,需指定如下三个属性:
android:drawable:指定截取的远的Drawble对象
android:clipOrientation:指定截取方向,可设置为水平,垂直。
android:gravity:指定截取时的对齐方式
例子:徐徐展开一张图片
<?xml version="1.0" encoding="UTF-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/shuangta" android:clipOrientation="horizontal" android:gravity="center"> </clip>
package org.crazyit.org; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.graphics.drawable.ClipDrawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ImageView; public class ClipDrawableTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageview = (ImageView) findViewById(R.id.image); //获取图片所显示的ClipDrawable对象 final ClipDrawable drawable = (ClipDrawable) imageview.getDrawable(); final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //如果该消息是本程序所发送的 if (msg.what == 0x1233) { //修改ClipDrawable的level值 drawable.setLevel(drawable.getLevel() + 200); } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Message msg = new Message(); msg.what = 0x1233; //发送消息,通知应用修改ClipDrawable对象的level值。 handler.sendMessage(msg); //取消定时器 if (drawable.getLevel() >= 10000) { timer.cancel(); } } } , 0 , 300); } }
5.AnimationDrawble资源动画资源,这类也很常见,也不想多说
-
Android 资源的使用之Drawble使用
最新推荐文章于 2023-12-07 11:46:59 发布