您的位置:首页 > 移动开发 > Android开发

Android画图学习总结(一)——类的简介

2012-06-06 16:32 435 查看
学习Android有一段时间了,看完了AndroidSDK中的大部分文档,但是始终感觉自己还缺少很多,后来发现,AndroidSDK中只是介绍了Google自己开发的那一部分如何使用,Android中引用至Java的部分如何使用却没有说明。当然这也不是Google的职责,但是这对我们C++程序员来说的确是缺少了很多,在这里我们将对Google“缺少的部分”并结合AndroidSDK中Reference说明来详细介绍,并不断的补充完善。

首先,如何获取res中的资源

数据包package:android.content.res

主要类:Resources

AndroidSDK中的简介:Classfor accessing an application’s resources.Class for accessing anapplication’s resources. This sits on top of the asset manager ofthe
application (accessible through getAssets()) and provides ahigher-level API for getting typed data from theassets.

其主要接口按照功能,划分为以下三部分:
getXXXX()

例如:

intgetColor(int id)

DrawablegetDrawable(int id)

StringgetString(int id)
直接获取res中存放的资源
InputStreamopenRawResource(int id)
获取资源的数据流,读取资源数据
voidparseBundleExtras(

XmlResourceParser parser, Bundle outBundle)
从XML文件中获取数据
Resource为每种资源提供了相应的接口来获取这种资源,除了可以直接获取资源外,还额外提供了以数据流的方式获取资源,这在以后的应用程序开发中会经常使用,那么如何获取Resources了,如下:Resourcesr
=this.getContext().getResources();

其次,如何获取资源中的画图对象

数据包package:android.graphics.drawable

主要类:Drawable

AndroidSDK中的简介:ADrawable is a general abstraction for “something that can bedrawn.” Most often you will deal with Drawable as the type ofresource retrieved
for drawing things to the screen; the Drawableclass provides a generic API for dealing with an underlying visualresource that may take a variety of forms.

看了以上简介,发现Drawable是个virtualclass,具体如何画图,需要具体分析Drawable的子类,例如:BitmapDrawable

AndroidSDK中的简介:ADrawable that wraps a bitmap and can be tiled, stretched, or aligned.You can create a BitmapDrawable from a file path, an input stream,through
XML inflation, or from a Bitmap object. It can be defined inan XML file with the <bitmap> element.

其主要接口如下:
BitmapDrawable()

BitmapDrawable(Bitmapbitmap)

BitmapDrawable(Stringfilepath)

BitmapDrawable(InputStreamis)
voiddraw(Canvas canvas)

Drawin its bounds (set via setBounds) respecting optional effects suchas alpha (set via setAlpha) and color filter (set viasetColorFilter).
finalBitmap getBitmap()
finalPaint getPaint()
Drawable是个抽象类,在BitmapDrawable中我们就看到位图的具体操作,在仔细看下BitmapDrawable的构造函数,我们就会发现与Resource中的openRawResource()接口是相对应的,就可以通过以下方法来获取位图:

Resourcesr = this.getContext().getResources();

Inputstream is =r.openRawResource(R.drawable.my_background_image);

BitmapDrawable bmpDraw = new BitmapDrawable(is);

Bitmap bmp =bmpDraw.getBitmap();

关于Drawable深入的学习与理解,请阅读Android画图学习总结(三)——Drawable

然后,看几个常用的辅助类

Paint

数据包package:android.graphics

AndroidSDK中的简介:ThePaint class holds the style and color information about how to drawgeometries, text and bitmaps.
主要就是定义:画刷的样式,画笔的大小/颜色等。

Typeface

数据包package:android.graphics

AndroidSDK中的简介:TheTypeface class specifies the typeface and intrinsic style of a font.主要就是定义:字体。

最后,核心类显示资源

数据包package:android.graphics

主要类:Canvas

AndroidSDK中的简介:TheCanvas class holds the “draw” calls. To draw something, you need4 basic components: A Bitmap to hold the pixels, a Canvas to host thedraw
calls (writing into the bitmap), a drawing primitive (e.g. Rect,Path, text, Bitmap), and a paint (to describe the colors and stylesfor the drawing).

按照结构的功能,将主要接口分为以下3部分:
booleanclipXXXX()
Region区域操作:

DIFFERENCE

INTERSECT

REPLACE

REVERSE_DIFFERENCE

UNION

XOR
voiddrawXXXX()
画图函数
voidrotate()

void scale()

void skew()

void translate()
画布操作函数
Region在这里需要特殊说明下:Region就是一个区域,也就是画布(Canvas)中的有效区域,在无效区域上draw,对画布没有任何改变。

总结说明

在写代码前,必须先仔细看下这几个主要的类,在这里我也只是把SDK中的介绍稍微总结下,它代替不了你对SDK的详细阅读,毕竟SDK是最详细的说明文档,在后续篇幅中再深入详细的介绍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: