易采站长站为您分析android中图形图像处理之drawable用法,较为详细的分析了Android中绘图所涉及的各种常用技巧与方法,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了android中图形图像处理之drawable用法。。具体如下:
一、如何获取 res 中的资源
数据包package:android.content.res
主要类:Resources
其主要接口按照功能,划分为以下三部分:
getXXXX()
例如:
int getColor(int id)
Drawable getDrawable(int id)
String getString(int id) 直接获取res中存放的资源
InputStream openRawResource(int id) 获取资源的数据流,读取资源数据
void parseBundleExtras(XmlResourceParser parser, Bundle outBundle) 从XML文件中获取数据
Resource为每种资源提供了相应的接口来获取这种资源,除了可以直接获取资源外,还额外提供了以数据流的方式获取资源,这在以后的应用程序开发中会经常使用,那么如何获取Resources了,如下:Resources r = this.getContext().getResources();
二、如何获取资源中的画图对象
数据包package:android.graphics.drawable
主要类:Drawable
Drawable是个virtual class,具体如何画图,需要具体分析Drawable的子类,例如:BitmapDrawable
其主要接口如下:
BitmapDrawable()
BitmapDrawable(Bitmap bitmap)
BitmapDrawable(String filepath)
BitmapDrawable(InputStream is)
void draw(Canvas canvas)
final Bitmap getBitmap()
final Paint getPaint()
Drawable是个抽象类,在BitmapDrawable中我们就看到位图的具体操作,在仔细看下BitmapDrawable的构造函数,我们就会发现与Resource中的openRawResource()接口是相对应的,就可以通过以下方法来获取位图:
- Resources r = this.getContext().getResources(); Inputstream is = r.openRawResource(R.drawable.my_background_image);
- BitmapDrawable bmpDraw = new BitmapDrawable(is); Bitmap bmp = bmpDraw.getBitmap();










