Android开发艺术探索<Drawable系列之一BitmapDrawable>

来源:互联网 发布:自学数据挖掘 编辑:程序博客网 时间:2024/06/06 06:08

简介:

Drawable表示一种图像的概念,但不完全指的是图片,通过颜色也可以构造出图像的效果。Drawable的宽高属性比较重要,获取方法如下。

注意:一张图片形成的Drawable,它的内部宽高就是图片的宽高,但是颜色形成的宽高,就没有这个概念,所以默认值都会返回-1,一般来说,Drawable没有大小概念,当用作View背景时,Drawable在显示的时候会被拉伸到View的同等大小。

/*** Returns the drawable's intrinsic width.* <p>* Intrinsic width is the width at which the drawable would like to be laid* out, including any inherent padding. If the drawable has no intrinsic* width, such as a solid color, this method returns -1.** @return the intrinsic width, or -1 if no intrinsic width*/publicint getIntrinsicWidth(){return-1;}/*** Returns the drawable's intrinsic height.* <p>* Intrinsic height is the height at which the drawable would like to be* laid out, including any inherent padding. If the drawable has no* intrinsic height, such as a solid color, this method returns -1.** @return the intrinsic height, or -1 if no intrinsic height*/publicint getIntrinsicHeight(){return-1;}



今天先来看BitMapDrawable:

BitmapDrawable表示的就是一张图片,可以直接引用最原始的图片,也可以通过XML来描述,对应的是<bitmap>标签。用XML描述的语法如下:

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:antialias="true"    android:dither="true"    android:filter="true"    android:gravity="fill"    android:mipMap="true"    android:tileMode="clamp"    android:src="@mipmap/ic_launcher"></bitmap>

相关属性分析:
  • android:src                 图片的资源ID
  • android:antialias        开启抗锯齿功能,让图片变平滑,一定程度降低图片清晰度,但是可以忽略不计,一般都开启。
  • android:dither            是否开启抖动效果。当图片的像素配置和手机屏幕的像素配置不一致的时候,开启选项可以让高质量的图片在低质量的屏幕上保持较好的效果,图片不会过于失真。                                       在Android中创建BitMap一般会选择ARGB8888模式,即ARGB4个通道各占8位,这种色彩模式下,一个像素所占的大小为4个字节,一个像素的位数总和越高,图                                      像也就越逼真。一般都开启
  • android:filter              是够开启过滤效果,当图片尺寸被拉伸或者被压缩时,开启过滤效果可以保持较好的显示效果。
  • android:mipMap        图片相关处理技术,纹理映射,使用不多。
比较重要的两个属性是gravity和tileMode

  • android:gravity           当图片小于容器的尺寸时,此选项对图片进行定位,不同的选项可以用 “ | ” 组合使用。
top图片放在容器顶部,不改变图大小bottom图片放在容器底部,不改变图片大小left图片放在容器左部,不改变图片大小right图片放在容器右部,不改变图片大小center
图片在水平和垂直方向居中,不改变大小fill图片在水平和竖直方向填充容器,这就是默认值center_vertical图片竖直居中,不改变大小fill_vertical图片竖直方向填充容器center_horizontal图片水平方向居中,不改变大小fill_horizontal图片水平方向填充容器clip_vertical竖直方向的裁剪,少使用clip_horizontal水平方向的裁剪,较少使用
  • android:tileMode   平铺模式:选项有dsiable,clamp,repeat,mirror
  • disable是默认值,关闭平铺模式。
  • 当开启平铺模式后,gravity属性会被忽略。
repeat模式就是简单的平铺:     
                               
mirror是一种镜像的平铺:
 
clamp是将图片四周的像素扩展到周围区域


0 0
原创粉丝点击