學習Android 有一段時間了,看完了Android SDK中的大部分文檔,但是始終感覺自己還缺少很多,後來發現,Android SDK中只是介紹了Google自己開發的那一部分如何使用,Android中引用至Java的部分如何使用卻沒有說明。當然這也不是Google的職責,但是這對我們C++程序員來說的確是缺少了很多,在這裏我們將對Google“缺少的部分”並結合Android SDK中Reference說明來詳細介紹,並不斷的補充完善。
首先,如何獲取 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 <bitmap> 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
- Paint
數據包package:android.graphics
Android SDK中的簡介:The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 主要就是定義:畫刷的樣式,畫筆的大小/顏色等。 - 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是最詳細的說明文檔,在後續篇幅中再深入詳細的介紹。