原文链接:http://blog.csdn.net/lonelyroamer/article/details/8147542
这个写的不错,记录一下方便以后查看,感谢作者
一、如何创建一个BitmapDrawable对象
Bitmap,代表一个位图图像,Android支持三种格式的位图图像:.png (preferred),.jpg (acceptable), .gif (discouraged)。
括号里的说明,代表这三种格式的图片在Android中的支持情况,.png格式图片优先,.jpg格式也可以,但是效果没有.png好,.gif支持最差。
可以直接使用图片的名称作为资源ID,来直接引用一个位图图片。也可以再XML文件中创建一个资源别名的ID。
在构建应用的时候,Bitmap文件可能会被appt工具压缩自动优化为无损图像。例如,一个真彩色PNG,不需要超过256的颜色可以被转换成一个8位PNG和调色板。这将导致一个图像质量相同,但这需要更少的内存。所以要意识到,在drawable目录中图像的二进制文件在构建程序时可以改变。如果你打算读一个图像作为字节流并将它转换成一个位图,把你的图片放在在res /raw/文件夹里,在那里他们不会被优化。
1、通过Bitmap File
一个bitmap文件就是一个.png、.jpg,.gif格式的文件。Android会对存储在res/drawable/目录下的这些文件创建一个Drawable资源。
文件位置:
res/drawable/filename.png
(.png
, .jpg
, or .gif
) 文件名即资源的ID。
编译资源数据类型:
指向BitmapDrawable类型的指针。
资源引用:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
示例:
-
有这样存储的图片
res/drawable/myimage.png
,在layout xml文件中将他显示在视图上。
<span class="tag"><ImageView</span><span class="pln"> </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln"> </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln"> </span><span class="atn">android:src</span><span class="pun">=</span><span class="atv">"@drawable/myimage"</span><span class="pln"> </span><span class="tag">/></span>
在java代码中检索出为一个Drawable对象。
- Resources res=getResources();
- Drawable drawable = res.getDrawable(R.drawable.<span class="atv">myimage</span>);
- //实际上这是一个BitmapDrawable对象
- BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;
- //可以在调用getBitmap方法,得到这个位图
- Bitmap bitmap=bitmapDrawable.getBitmap();
参考:
2、通过XML Bitmap
一个XML bitmap是一个在XML文件中定义的指向一个bitmap文件的资源。其效果是作为一个原始位图文件的别名,并且可以指定一些额外的属性。
注意:你可以在<item>节点中使用<bitmap>作为它的子节点。比如,当你定义一个state list或者layer list的时候,可以包括一个android:drawable
属性
Note: You can use a <bitmap>
element as a child of an<item>
element. Forexample, when creating astate list orlayer list,you can exclude theandroid:drawable
attribute from an<item>
element and nest a<bitmap>
inside it that defines the drawable item.
-
文件位置:
-
res/drawable/filename.xml
filename作为资源的ID
编译资源类型
- 指向BitmapDrawable类型的指针 资源引用
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
语法:
-
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">bitmap</a><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="atn">android:src</span><span class="pun">=</span><span class="atv">"@[package:]drawable/</span><em>drawable_resource</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:antialias</span><span class="pun">=</span><span class="atv">["true"</span><span class="pln"> | </span><span class="atv">"false"</span><span class="pln">] </span><span class="atn">android:dither</span><span class="pun">=</span><span class="atv">["true"</span><span class="pln"> | </span><span class="atv">"false"</span><span class="pln">] </span><span class="atn">android:filter</span><span class="pun">=</span><span class="atv">["true"</span><span class="pln"> | </span><span class="atv">"false"</span><span class="pln">] </span><span class="atn">android:gravity</span><span class="pun">=</span><span class="atv">["top"</span><span class="pln"> | </span><span class="atv">"bottom"</span><span class="pln"> | </span><span class="atv">"left"</span><span class="pln"> | </span><span class="atv">"right"</span><span class="pln"> | </span><span class="atv">"center_vertical"</span><span class="pln"> | </span><span class="atv">"fill_vertical"</span><span class="pln"> | </span><span class="atv">"center_horizontal"</span><span class="pln"> | </span><span class="atv">"fill_horizontal"</span><span class="pln"> | </span><span class="atv">"center"</span><span class="pln"> | </span><span class="atv">"fill"</span><span class="pln"> | </span><span class="atv">"clip_vertical"</span><span class="pln"> | </span><span class="atv">"clip_horizontal"</span><span class="pln">] </span><span class="atn">android:tileMode</span><span class="pun">=</span><span class="atv">["disabled"</span><span class="pln"> | </span><span class="atv">"clamp"</span><span class="pln"> | </span><span class="atv">"repeat"</span><span class="pln"> | </span><span class="atv">"mirror"</span><span class="pln">] </span><span class="tag">/></span>
节点介绍:
- 示例:
-
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><bitmap</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="atn">android:src</span><span class="pun">=</span><span class="atv">"@drawable/icon"</span><span class="pln"> </span><span class="atn">android:tileMode</span><span class="pun">=</span><span class="atv">"repeat"</span><span class="pln"> </span><span class="tag">/></span>
参考:
二、BitmapDrawable的使用
一个BitmapDrawable就是封装了一个位图。直接以文件的方式,就是封装了一个原始的位图。以Xml方式,可以对原始的位图进行一系列的处理,比如说抗锯齿,拉伸,对齐等等。
要了解BitmapDrawable的使用,还需要明白Bitmap、BitmapFactory等类。Bitmap代表了一个原始的位图,并且可以对位图进行一系列的变换操作。BitmapFactory提供一系列的方法用于产生一个Bitmap对象。多用在Canvas中。
关于绘图和位图变换以后再学习。BitmapDrawable的使用比较简单,就是在其他的xml文件中直接引用就可以了,不过要注意在xml中定义BitmapDrawable各个属性使用和含义。