手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android UI开发专题(三) 各种Drawable
Android UI开发专题(三) 各种Drawable
摘要:本次我们主要讲解Android平台下的各种Drawable,这里在SDK的android.graphics.drawable包下面可以看到有...

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

一、AnimationDrawable

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

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

Code highlighting produced by Actipro CodeHighlighter (freeware)

/

-->1

2

3

4

5

6

7

8animation-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中的相关内容。

【Android UI开发专题(三) 各种Drawable】相关文章:

android 引导界面的实现方法

Android开发之声明周期Activity Lifecycle

Android中3种图片压缩处理方法

Android 动画之ScaleAnimation应用详解

Android开发之OpenGL ES 基础

android,不显示标题的方法小例子

Android中Service(后台服务)详解

android 线性布局LinearLayout实例代码

解析Android开发优化之:从代码角度进行优化的技巧

Android开发之多个Activity间的交互

精品推荐
分类导航