Android UI開發專題(一) 之介面設計
近期很多網友對Android使用者介面的設計表示高度興趣,對於Android UI開發自繪控制項和遊戲製作而言掌握好繪圖基礎是必不可少的。本次專題分10節來講述,有關OpenGL ES相關的可能將放到以後再透露。本次主要涉及以下四個包的相關內容:
android.content.res 資源類
android.graphics 底層圖形類
android.view 顯示類
android.widget 控制項類
一、android.content.res.Resources
對於Android平台的資源類android.content.res.Resources可能很多網友比較陌生,一起來看看SDK上是怎麼介紹的吧,Contains classes for accessing application resources, such as raw asset files, colors, drawables, media or other other files in the package, plus important device configuration details (orientation, input types, etc.) that affect how the application may behave.平時用到的二進位源檔案raw、顏色colors、圖形drawables和多媒體檔案media的相關資源均通過該類來管理。
int getColor(int id) 對應res/values/colors.xml
Drawable getDrawable(int id) 對應res/drawable/
XmlResourceParser getLayout(int id) 對應res/layout/
String getString(int id) 和CharSequence getText(int id) 對應res/values/strings.xml
InputStream openRawResource(int id) 對應res/raw/
void parseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle) 對應res/xml/
String[] getStringArray(int id) res/values/arrays.xml
float getDimension(int id) res/values/dimens.xml
二、android.graphics.Bitmap
作為位元影像操作類,Bitmap提供了很多實用的方法,常用的我們總結如下:
boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 壓縮一個Bitmap對象根據相關的編碼、畫質儲存到一個OutputStream中。其中第一個壓縮格式目前有JPG和PNG
void copyPixelsFromBuffer(Buffer src) 從一個Buffer緩衝區複製位元影像像素
void copyPixelsToBuffer(Buffer dst) 將當前位元影像像素內容複寫到一個Buffer緩衝區
我們看到建立位元影像對象createBitmap包含了6種方法在目前的Android 2.1 SDK中,當然他們使用的是API Level均為1,所以說從Android 1.0 SDK開始就支援了,所以大家可以放心使用。
static Bitmap createBitmap(Bitmap src)
static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config)
static Bitmap createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
static Bitmap createBitmap(int width, int height, Bitmap.Config config)
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)
static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) //建立一個可以縮放的位元影像對象
final int getHeight() 擷取高度
final int getWidth() 擷取寬度
final boolean hasAlpha() 是否有透明通道
void setPixel(int x, int y, int color) 設定某像素的顏色
int getPixel(int x, int y) 擷取某像素的顏色,android開發網提示這裡返回的int型是color的定義
三、android.graphics.BitmapFactory
作為Bitmap對象的I/O類,BitmapFactory類提供了豐富的構造Bitmap對象的方法,比如從一個位元組數組、檔案系統、資源ID、以及輸入資料流中來建立一個Bitmap對象,下面本類的全部成員,除了decodeFileDescriptor外其他的重載方法都很常用。
static Bitmap decodeByteArray(byte[] data, int offset, int length) //從位元組數組建立
static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)
static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) //從檔案建立,路徑要寫全
static Bitmap decodeFile(String pathName)
static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts) //從輸入資料流控制代碼建立
static Bitmap decodeFileDescriptor(FileDescriptor fd)
static Bitmap decodeResource(Resources res, int id) //從Android的APK檔案資源中建立,android123提示是從/res/的drawable中
static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts)
static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
static Bitmap decodeStream(InputStream is) //從一個輸入資料流中建立
static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
四、android.graphics.Canvas
從J2ME MIDLET時我們就知道Java提供了Canvas類,而目前在Android平台中,它主要任務為管理繪製過程,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).