Home
JavaEE
Java GUI
Framework
DHtml
Android
插画
留言本
  • 哈哈,老婆怀上了!婚礼不变,还是在正月十二!
  • 看看android扫雷游戏的主要代码
  • 我用android sdk做的一个扫雷游戏
  • Android中给Spinner或ListView添加数据
  • 在android的sdk中动态添加view的方法
  • 必须转载的一篇非常感人的文章!
  • 三月雨后大王山下娇艳欲滴的杜鹃
  • s60v3的6120c与蓝牙耳机nokia BH-320的配对连接方法
  • Android中用Bitmap获取图片中某个区域的图象
  • Android中AnimationDrawable实现简单动画的例子
  • 在客户端IE中使用jasperView,客户端显示JasperViewer
  • jspsmartupload取得参数的值是乱码的问题
  • 客户端显示JasperViewer并在Jasperview的applet中加超链接
  • Hibernate基于链接表的多对多many-to-many双向关联例子
  • Hibernate双主键的设置例子
  • Hibernate中的properties和formula的用法
  • Android中,Drawable接口及其子类的使用方法
  • Android中用Bitmap获取图片中某个区域的图象
  • Android中AnimationDrawable实现简单动画的例子
  • Animation与Interpolator及AnimationSet的用法
Android中,Drawable接口及其子类的使用方法
posted by David Chen at May 6, 2009, 5:22 PM

        A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with
Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides
a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View,
a Drawable does not have any facility to receive events or otherwise interact with the user.
Drawable是一个可画对象,可以用它在屏膜上画内容,也可以直接取得已的图等。
Drawable d = this.getResources().getDrawable(R.drawable.a1);
// this指代Activity, R.drawable.a1是在\res\drawable文件夹中的名称为a1的图。

常见的几种Drawable对象类型:
Bitmap: the simplest Drawable, a PNG or JPEG image.
// 一般用于处理jpg和png图

Nine Patch: an extension to the PNG format allows it to specify information about
how to stretch it and place things inside of it.
// 用于如何缩放和放要想你的东西到图里面的扩展的PNG格式

Shape: contains simple drawing commands instead of a raw bitmap, allowing it to
resize better in some cases.

Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
LayerDrawable(Drawable[] array);
用于图层方式存取多个Drawable,可以用getDrawable(int index)取得其中一个Drawable,对应setLayer(int);

States: a compound drawable that selects one of a set of drawables based on its state.
addState(int[] stateSet, Drawable drawable);
An array of resource Ids to associate with the image. Switch to this image by calling setState().
为不同的状态存取不同的Drawable,通过指定状态的id值,可以取得如获得焦点,失去焦点等时的不同图像
如:addState( new int[]{R.attr.state_focused, R.attr.state_pressed}, ... ); 对应setState(int[]);

Levels: a compound drawable that selects one of a set of drawables based on its level.
addLevel(int low, int high, Drawable drawable)
可以指定在不同的级别中显示不同的图
如:addLevel(1, 3, ...); // 在第1到3级的时候显示相应的图,对应setLevel(int)


Scale: a compound drawable with a single child drawable, whose overall size is
modified based on the current level.
ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight)
// 这是一个可以缩放的drawable,可以将图缩放到指定的大小

例:
Drawable[] array = new Drawable[] {
        this.getResources().getDrawable(R.drawable.a1),
        this.getResources().getDrawable(R.drawable.a2),
        this.getResources().getDrawable(R.drawable.a3),
        this.getResources().getDrawable(R.drawable.a4)
};
LayerDrawable ld = new LayerDrawable( array );

ImageButton imgBtn = new ImageButton( this );
imgBtn.setImageDrawable( ld.getDrawable(2) );
// 显示a3这个图
Labels:   Java    Android  
Trackback:   http://cwq.iou1314.com/android-drawable_a353
本站内的任何文章,只代表个人意见或学习所用,如有版权声明,请尊重作者的劳动成果,在转载时请保留原始链接并注明出处。
© 2007 - 2010 CWQ.IOU1314.COM All Rights Reserved.