編輯: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 檔中取得資料
Resources 為每種資源提供了相應的介面來取得該資源。除了可直接取得資源外,還額外提供了以資料流方式取得資源的途徑,這在日後的應用程式開發中會經常使用。那麼如何取得 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 的建構函式,會發現它與 Resources 中的 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. 主要就是定義字體。
核心類:顯示資源
資料包 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).
依結構功能,將主要介面分為以下三部分:
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 是一個通用的抽象類,它的目的是告訴你什麼東西是可以畫的。你會發現,各種繪圖類別都是基於 Drawable 擴展出來的(見下表),當然你也可以繼承它來建立自己的繪圖類。
有三種方法可以定義並例項化一個 Drawable:將圖片保存到工程資源中、使用 XML 檔描述 Drawable 屬性,或用一個正常的類去構造。以下我們將討論前兩種技術(對有開發經驗的人來說,構造並不是新鮮事)。
從資源圖像檔中建立
比較簡單的方法是加入一張圖片到你的程式,然後透過資源檔引用它,支援的檔案類型有 PNG(首選)、JPG(可接受)、GIF(不建議)。顯然,這對顯示應用程式圖標來說是首選方法,也可用來顯示 LOGO,其餘圖片則可運用在遊戲等場景。
把一個圖片資源加入到你工程的 res/drawable/ 目錄中,就能在程式碼或 XML 布局中引用它,也就是用資源編號來引用。比如選一個檔案,只要去掉副檔名即可(例如 my_image.png,引用時就是 my_image)。
注意:SDK 指出,為了縮小圖片的儲存空間,Build 時可能會對圖片進行壓縮;若不希望被壓縮,可將圖片放在 res/raw/ 目錄中。
SDK 給出的例子:
LinearLayout mLinearLayout; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a LinearLayout in which to add the ImageView mLinearLayout = new LinearLayout(this); // Instantiate an ImageView and define its properties ImageView i = new ImageView(this); i.setImageResource(R.drawable.my_image); i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // Add the ImageView to the layout and set the layout as the content view mLinearLayout.addView(i); setContentView(mLinearLayout); }獲取Drawable對象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable.my_image); 注意:保持每個資源類型的一致性,能確保你專案狀態的一致性,就不必擔心有多個不同類型的物件來例項化它。例如,如果使用相同的圖像資源來例項化兩個 Drawable 物件,然後修改其中一個 Drawable 的屬性(例如 alpha),不幸的是這個效果也會出現在另一個物件上。所以當處理同一資源的多個例項物件時,並非直接轉換為 Drawable,而是應該執行 tween animation。
如何將資源加入 ImageView:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:tint="#55ff0000" android:src="@drawable/my_image"/>
從 XML 檔中建立
至今,你應該比較熟悉依 Android 的原則去開發使用者介面,因此也應理解定義一個 XML 檔對於物件的作用與靈活性有多重要。這個理念在 Drawable 上無數次得到印證。
如果你想建立一個 Drawable 物件,而這個物件並不依賴變數或使用者的互動,把它定義到 XML 中應該是個不錯的方法。即使你期望日後在應用程式中改變其屬性來提升使用者體驗,也應考慮把物件放入 XML,因為你可以隨時修改其屬性。
當你在 XML 中定義了一個 Drawable,把這個 XML 檔保存到工程目錄下的 res/drawable 目錄中,然後透過呼叫 Resources.getDrawable() 來檢索並例項化,傳入 XML 檔中的資源 ID 號。任何 Drawable 的子類都支援 inflate 這個方法,它會透過 XML 來例項化你的程式;任何 Drawable 都支援 XML 擴展,以利用特殊的 XML 屬性來幫助定義物件屬性,可查閱任何 Drawable 子類的說明文件來了解如何定義 XML 檔。
以下定義了一個 TransitionDrawable:An extension of LayerDrawables that is intended to cross-fade between the first and second layer. It can be defined in an XML file with the <transition> element. Each Drawable in the transition is defined in a nested <item>. 有關 TransitionDrawable 的詳細資訊請參閱 https://androidappdocs.appspot.com/reference/android/graphics/drawable/TransitionDrawable.html。
將其定義在 res/drawable/expand_collapse.xml:
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/pic1"/> <item android:drawable="@drawable/pic2"/> </transition>
下面加以例項化並處理:
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res=getResources(); TransitionDrawable trans=(TransitionDrawable )res.getDrawable(R.drawable.expand_collapse); ImageView image = (ImageView)findViewById(R.id.ImageView01); image.setImageDrawable(trans); trans.startTransition(3000); } }ShapeDrawable 當你想畫一些動態的二維圖片時,ShapeDrawable 物件可能會對你有很大幫助。透過 ShapeDrawable,你可以用程式畫出任何你想像得到的圖像與樣式。
ShapeDrawable 繼承了 Drawable,所以你可以呼叫 Drawable 裡有的函式,比如透過 setBackgroundDrawable() 設為視圖的背景。當然,你也可以在自訂的視圖布局中畫你的圖形,因為 ShapeDrawable 有自己的 draw() 方法。你可以在 View.OnDraw() 方法期間建立一個視圖的子類去畫 ShapeDrawable。
ShapeDrawable 類(像許多其他位於 android.graphics.drawable 包的 Drawable 類型)允許你定義 drawable 公用方法的各種屬性。有些屬性你可能需要調整,包括透明度、顏色過濾、不透明度、顏色。
NinePatchDrawable
NinePatchDrawable 繪製的是一個可伸縮的位圖圖像,Android 會自動調整大小來容納顯示的內容。一個例子就是以 NinePatch 作為背景,使用標準的 Android 按鈕,按鈕必須伸縮來容納長度變化的字元。
NinePatchDrawable 是一個標準的 PNG 圖像,它包括額外的 1 個像素邊界,你必須將它保存為 .9.png 的副檔名,並保持到工程的 res/drawable 目錄中。
這個邊界是用來確定圖像的可伸縮與靜態區域。你可以在左邊和上邊的線上畫一個或多個黑色 1 像素的點,指出可伸縮的部分(你可能需要很多可伸縮部分),它的相對位置在可伸縮部分相同,所以大的部分總是保持很大。
你還可以在圖像的右邊和下邊畫一條可選的 drawable 區域(有效的內邊距線)。如果你的視圖物件將 NinePatch 設為背景,並指定特殊的視圖字體,它將自行伸縮,使所有文字來適應根據右線與底部線設計好的區域(如果有的話);當然內邊距線不包括其中,Android 可以使用左邊的線與上面的線來定義一個 drawable 區域。
我們來釐清這兩條不同的線:左邊與頂部的線用來定義哪些圖像的像素允許在伸縮時被複製;底部與右邊的線則用來定義一個相對位置內的圖像範圍,視圖的內容就放入其中。
希望本文所述能對大家的 Android 程式設計有所幫助。
Android 登入畫面設計與錯誤次數限制
登入畫面是許多 App 的第一道防線。設計時除了帳號與密碼欄位,也要考慮錯誤次數限制,避免有心人士不斷嘗試。 版面設計 使用兩個 EditText 分別輸入使用者名稱與密
Android 自訂 EditText 與 AutoCompleteTextView 的文字選取底色
多數 Android ROM 上,文字被選取時的背景色是清爽的鮮綠色,視覺上不難看。但某些三星機型卻是藍色,和整體介面放在一起格外突兀。好消息是,這個顏色完全可以透過程式
Android 資料備份:用 Backup Agent 把 App 資料存到雲端
Android 提供備份服務,讓你把應用程式的資料與設定備份到遠端「雲端」儲存空間,日後重灌或換機時就能還原到備份當下的狀態。要特別注意的是,這個機制只涵蓋 App 自身
Android 個人理財實作之一:專案架構與啟動畫面的實現
從這一篇開始,我們要一起動手做一個 Android 個人理財工具,並把整个開發歷程記錄下來,與大家分享。既然是系列的第一篇,先把全局藍圖交代清楚,再動手做第一塊拼圖——啟