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

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

数据包package:android.content.res

主要类:Resources

Android SDK中的简介:Class for accessing an application’s resources.Class for accessing an application’s resources. This sits on top of the asset manager of the application (accessible through getAssets()) and provides a higher-level API for getting typed data from the assets.

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

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

Android SDK中的简介:A Drawable is a general abstraction for “something that can be drawn.” Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms.

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

Android SDK中的简介:A Drawable 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 in an XML file with the element.

其主要接口如下:

BitmapDrawable()

BitmapDrawable(Bitmap bitmap)

BitmapDrawable(String filepath)

BitmapDrawable(InputStream is)

void draw(Canvas canvas) Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

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();

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

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

1. Paint

数据包package:android.graphics

Android SDK中的简介:The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 主要就是定义:画刷的样式,2. 画笔的大小/颜色等。

3. Typeface

数据包 package:android.graphics

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

最后,核心类显示资源

数据包package:android.graphics

主要类:Canvas

Android SDK中的简介:The Canvas class holds the “draw” calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

按照结构的功能,将主要接口分为以下3部分:

boolean clipXXXX() Region区域操作:

DIFFERENCE

INTERSECT

REPLACE

REVERSE_DIFFERENCE

UNION

XOR

void drawXXXX() 画图函数

void rotate()

void scale()                        画布操作函数

void skew()

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

总结说明

在写代码前,必须先仔细看下这几个主要的类,在这里我也只是把SDK中的介绍稍微总结下,它代替不了你对SDK的详细阅读,毕竟SDK是最详细的说明文档,在后续篇幅中再深入详细的介绍。

摘自:http://wenku.baidu.com/view/09146705eff9aef8941e0623.html?from=rec&pos=4&weight=38&lastweight=38&count=5

猜你喜欢

转载自blog.csdn.net/xu13879531489/article/details/80565522