• 爱情文章
  • 亲情文章
  • 友情文章
  • 生活随笔
  • 校园文章
  • 经典文章
  • 人生哲理
  • 励志文章
  • 搞笑文章
  • 心情日记
  • 英语文章
  • 范文大全
  • 作文大全
  • 新闻阅读
  • 当前位置: 山茶花美文网 > 范文大全 > 正文

    Android,UI开发专题(三),各种Drawable Android开发艺术探索

    时间:2020-05-22来源:山茶花美文网 本文已影响 山茶花美文网手机站

    本次我们主要讲解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中的相关内容。

    • 爱情文章
    • 亲情文章
    • 友情文章
    • 随笔
    • 哲理
    • 励志
    • 范文大全