資料包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 <code><bitmap></code> 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();
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. 主要就是定義:字型。
主要類: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,對畫布沒有任何改變。
Drawable是一個通用的抽象類,它的目的是告訴你什麼東西是可以畫的。你會發現基于Drawable類擴充出各種繪圖的類,見下面的表格,當然你可以繼承它來建立你自己的繪圖類.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/Animatable.html">Animatable</a>
Interface that drawables suporting animations should implement.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/Drawable.Callback.html">Drawable.Callback</a>
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/AnimationDrawable.html">AnimationDrawable</a>
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/BitmapDrawable.html">BitmapDrawable</a>
A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/ClipDrawable.html">ClipDrawable</a>
A Drawable that clips another Drawable based on this Drawable's current level value.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/ColorDrawable.html">ColorDrawable</a>
A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/Drawable.html">Drawable</a>
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.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/Drawable.ConstantState.html">Drawable.ConstantState</a>
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/DrawableContainer.html">DrawableContainer</a>
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/DrawableContainer.DrawableContainerState.html">DrawableContainer.DrawableContainerState</a>
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/GradientDrawable.html">GradientDrawable</a>
A Drawable with a color gradient for buttons, backgrounds, etc.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/InsetDrawable.html">InsetDrawable</a>
A Drawable that insets another Drawable by a specified distance.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/LayerDrawable.html">LayerDrawable</a>
A Drawable that manages an array of other Drawables.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/LevelListDrawable.html">LevelListDrawable</a>
A resource that manages a number of alternate Drawables, each assigned a maximum numerical value.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/NinePatchDrawable.html">NinePatchDrawable</a>
A resizeable bitmap, with stretchable areas that you define.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/PaintDrawable.html">PaintDrawable</a>
Drawable that draws its bounds in the given paint, with optional rounded corners.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/PictureDrawable.html">PictureDrawable</a>
Drawable subclass that wraps a Picture, allowing the picture to be used whereever a Drawable is supported.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/RotateDrawable.html">RotateDrawable</a>
A Drawable that can rotate another Drawable based on the current level value.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/ScaleDrawable.html">ScaleDrawable</a>
A Drawable that changes the size of another Drawable based on its current level value.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/ShapeDrawable.html">ShapeDrawable</a>
A Drawable object that draws primitive shapes.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/ShapeDrawable.ShaderFactory.html">ShapeDrawable.ShaderFactory</a>
Base class defines a factory object that is called each time the drawable is resized (has a new width or height).
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/StateListDrawable.html">StateListDrawable</a>
Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.
<a href="http://androidappdocs.appspot.com/reference/android/graphics/drawable/TransitionDrawable.html">TransitionDrawable</a>
An extension of LayerDrawables that is intended to cross-fade between the first and second layer.
有三種方法可以定義和執行個體化一個Drawable:儲存一個圖檔到你工程資源中,使用XML檔案來描述Drawable屬性或者用一個正常的類去構造。下面我們将讨論兩種技術(對一個有開發經驗的開發者來說構造并不是最新的技術)。
一個比較簡單的方法是添加一個圖檔到你的程式中,然後通過資源檔案引用這個檔案,支援的檔案類型有PNG(首選的) JPG(可接受的)GIF(不建議),顯然這種對于顯示應用程式的圖示跟來說是首選的方法,也可以用來顯示LOGO,其餘的圖檔可以用在例如遊戲中。
把一個圖檔資源,添加你的檔案到你工程中res/drawable/目錄中去,從這裡,你就可以引用它到你的代碼或你的XML布局中,也就是說,引用它也可以用資源編号,比如你選擇一個檔案隻要去掉字尾就可以了(例如:my_image.png 引用它是就是my_image)。
注意:SDK指出,為了縮小圖檔的存儲空間,在Build的時候又可能對圖檔進行壓縮,如果不想被壓縮,可以将圖檔放在res/raw/目錄中。
SDK給出的例子:
擷取Drawable對象:
注意:保持每個資源類型的一至,可以保證你項目狀态的一緻性,就不用擔心有許多不同類型的對象來執行個體化它。例如:如果使用相同的圖像資源來執行個體化兩個Drawable對象。然後修改一個Drawables的屬性(例如alpha),然後不幸得是這個效果也會出現在另一個對象上去。是以當處理同一個資源的多個執行個體對象時,不是直接轉換為Drawable,而是應該執行tween animation
到如今,你應該比較熟悉按Android的原則去開發一個使用者接口,是以,你也應該了解了定義一個XML檔案對于對象的作用與靈活的重要性。這個理念無數次用于Drawables.
如果你想建立一個Drawable對象,而這個對象并不依賴于變量或使用者的交換,把它定義到XML中去應該是一個不錯的方法。即使你期望在你的應用程式中改變其屬性來增加使用者體驗。你應該考慮把對象放入XML中,因為你可以随時修改其屬性。
當你在你的XML中定義了一個Drawable,儲存這個XML檔案到你工程目錄下res/drawable目錄中,然後通過調用Resource.getDrawable()來檢索并執行個體化,傳遞給它XML檔案中的資源ID号。任何Drawable的子類都支援inflate這個方法,這個方法會通過XML來執行個體化你的程式。任何Drawable都支援XML的擴充來利用特殊的XML屬性來幫助定義對象的屬性,可以檢視任何Drawable子類文檔來看如何定義XML檔案。
将其定義在res/drawable/expand_collapse.xml:
下面執行個體化并處理:
當你想去畫一些動态的二維圖檔,一個ShapeDrawable對象可能會對你有很大的幫助。通過ShapeDrawable,你可以通過程式設計畫出任何你想到的圖像與樣式。
ShapeDrawable繼承了Drawable, 是以你可以調用Drawable裡有的函數,比如視圖的背景,通過setBackgroundDrawable()設定。當然,你可以在自定義的視圖布局中畫你的圖形,因為ShapeDrawable有自己的draw()方法。你可以在View.OnDraw()方法期間建立一個視圖的子類去畫ShapeDrawable。
ShapeDrawable類(像很多其他Drawable類型在android.graphics.drawable包)允許你定義drawable公共方法的各種屬性。有些屬性你可以需要調整,包括透明度,顔色過濾,不透明度,顔色。
例子:
顯示:
在XML中定義方法:如果你想用XML檔案配置來取代原有布局來畫自定義的drawable,于是你自定義的Drawable類必須重載view (Context, AttributeSet) 構造函數。
NinePatchDrawable 繪畫的是一個可以伸縮的位圖圖像,Android會自動調整大小來容納顯示的内容。一個例子就是NinePatch為背景,使用标準的Android按鈕,按鈕必須伸縮來容納長度變化的字元
NinePatchDrawable是一個标準的PNG圖像,它包括額外的1個像素的邊界,你必須儲存它字尾為.9.png,并且保持到工程的res/drawable目錄中。
這個邊界是用來确定圖像的可伸縮和靜态區域。你可以在左邊和上邊的線上畫一個或多個黑色的1個像素指出可伸縮的部分(你可以需要很多可伸縮部分),它的相對位置在可伸縮部分相同,是以大的部分總是很大的。
你還有可以在圖像的右邊和下邊畫一條可選的drawable區域(有效的,内邊距線)。如果你的視圖對象設定NinePath為背景然後指定特殊的視圖字型,它将自行伸縮使所有的文本來适應根據右線與底部線設計好的區域(如果有的話),當然内邊距線不包括其中,Android可以使用左邊的線與上面的線來定義一個drawable區域。
我們來澄清一下這兩條不同的線,左邊跟頂部的線來定義哪些圖像的像素允許在伸縮時被複制。底部與右邊的線用來定義一個相對位置内的圖像,視圖的内容就放入其中。

使用方法:
本文轉自feisky部落格園部落格,原文連結:http://www.cnblogs.com/feisky/archive/2010/01/08/1642567.html,如需轉載請自行聯系原作者