`
韩悠悠
  • 浏览: 827571 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Drawable

 
阅读更多

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.
//
一般用于处理jpgpng

Nine Patch: an extension to the PNG format allows it to specify information about
how to stretch it and place things inside of it.

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, ...); // 在第13级的时候显示相应的图,对应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) );

 本次我们主要讲解Android平台下的各种Drawable,这里在SDKandroid.graphics.drawable包下面可以看到有各种Drawable类多达十几种,它们到底之间有什么关系和区别呢?

  一、AnimationDrawable

  顾名思义该类主要表示动画的图形类,可以实现逐帧播放的效果,下面代码示例如下

  1. 定义一个cwj_animation.xml 放到res/drawable 目录下,其中定义的属性duration为延时,单位为毫秒,而oneshot属性表示是否仅播放一次,内容为:

1 <animation-list android:id="selected" android:oneshot="false">
2 <item android:drawable="@drawable/cwj0" android:duration="30" />
3 <item android:drawable="@drawable/cwj1" android:duration="30" />
4 <item android:drawable="@drawable/cwj2" android:duration="30" />
5 <item android:drawable="@drawable/cwj3" android:duration="30" />
6 <item android:drawable="@drawable/cwj4" android:duration="30" />
7 <item android:drawable="@drawable/cwj5" android:duration="30" />
8 </animation-list>
9
10

       2.java中调用也很简单

  ImageView img = (ImageView)findViewById(R.id.cwj_image); //首先声明一个ImageView对象在xml布局文件中

  img.setBackgroundResource(R.drawable.cwj_animation); //我们刚才的animation定义的xml文件

  AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); //构造AnimationDrawable对象

  frameAnimation.start() //开始播放动画

  3. AnimationDrawable类还提供了一些常用的方法如下:

  void stop() 停止

  void addFrame(Drawable frame, int duration) 添加一帧,类似xml中的布局

  Drawable getFrame(int index) 返回某帧的Drawable图形

  int getNumberOfFrames() 返回总共动画帧数

  boolean isOneShot() 是否仅播放一次

  boolean isRunning() 是否正在播放

  二、BitmapDrawable

  在Android平台中对于缩放、变形的Bitmap对象由BitmapDrawable类表示,其构造方法也很简单,由于该类继承于android.graphics.drawable.Drawable,相对Drawable而言提供了更多的有关位图的操作方法,主要的构造方法如下:

  BitmapDrawable() //直接构造一个空的对象,这样方式不推荐使用,SDK标记为deprecated.未来可能无法使用。

  BitmapDrawable(Resources res) //从资源中构造

  BitmapDrawable(Bitmap bitmap) //Bitmap对象直接构造,但也是不推荐,而是希望用下一种

  BitmapDrawable(Resources res, Bitmap bitmap) //bitmap中创建设置初始的分辨率从res

  BitmapDrawable(String filepath) //从具体文件路径构造,也不推荐使用,而是下一种更好

  BitmapDrawable(Resources res, String filepath) //同上

  BitmapDrawable(InputStream is) //从输入流中构造,同样推荐下面的方法

  BitmapDrawable(Resources res, InputStream is) //同上

  在BitmapDrawable类中相对于Drawable类主要新增了以下几种方法,均比较实用:

  final Bitmap getBitmap() 获取一个Bitmap对象

  int getOpacity() //获取透明度

  void setAntiAlias(boolean aa) //是否抗锯齿

  void setTargetDensity(Canvas canvas) //设置目标Canvas密度

  void setTargetDensity(DisplayMetrics metrics)

  三、ClipDrawable

  ColorDrawable

  Drawable

  GradientDrawable

  InsetDrawable

  LayerDrawable

  LevelListDrawable

  NinePatchDrawable

  PaintDrawable

  PictureDrawable

  RotateDrawable

  ScaleDrawable

  ShapeDrawable

  StateListDrawable

  TransitionDrawable

  以上的类型在常见的开发一般较少出现,主要是基类构造使用,Android内部的多个Widget基础控件使用了,感兴趣的网友可以查看开源GIT中的相关内容。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics