近期很多網友對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). 該類主要提供了三種構造方法,分別為構造一個空的Canvas、從Bitmap中構造和從GL對象中建立,如下 Canvas() Canvas(Bitmap bitmap) Canvas(GL gl) 同時Canvas類的一些欄位儲存著重要的繪製方法定義,比如Canvas.HAS_ALPHA_LAYER_SAVE_FLAG 儲存時需要alpha層,對於Canvas類提供的方法很多,每個都很重要,下面我們一一作介紹 boolean clipPath(Path path) boolean clipPath(Path path, Region.Op op) boolean clipRect(float left, float top, float right, float bottom) boolean clipRect(Rect rect) boolean clipRect(float left, float top, float right, float bottom, Region.Op op) boolean clipRect(Rect rect, Region.Op op) boolean clipRect(RectF rect) boolean clipRect(RectF rect, Region.Op op) boolean clipRect(int left, int top, int right, int bottom) boolean clipRegion(Region region, Region.Op op) boolean clipRegion(Region region) void concat(Matrix matrix) void drawARGB(int a, int r, int g, int b) void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint) void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint) void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) void drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, Paint paint) void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint) void drawCircle(float cx, float cy, float radius, Paint paint) void drawColor(int color) void drawColor(int color, PorterDuff.Mode mode) void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) void drawLines(float[] pts, Paint paint) void drawLines(float[] pts, int offset, int count, Paint paint) void drawOval(RectF oval, Paint paint) void drawPaint(Paint paint) void drawPath(Path path, Paint paint) void drawPicture(Picture picture, RectF dst) void drawPicture(Picture picture, Rect dst) void drawPicture(Picture picture) void drawPoint(float x, float y, Paint paint) void drawPoints(float[] pts, int offset, int count, Paint paint) void drawPoints(float[] pts, Paint paint) void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) void drawPosText(String text, float[] pos, Paint paint) void drawRGB(int r, int g, int b) void drawRect(RectF rect, Paint paint) void drawRect(float left, float top, float right, float bottom, Paint paint) void drawRect(Rect r, Paint paint) void drawRoundRect(RectF rect, float rx, float ry, Paint paint) void drawText(String text, int start, int end, float x, float y, Paint paint) void drawText(char[] text, int index, int count, float x, float y, Paint paint) void drawText(String text, float x, float y, Paint paint) void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint) void drawVertices(Canvas.VertexMode mode, int vertexCount, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, Paint paint) static void freeGlCaches() boolean getClipBounds(Rect bounds) final Rect getClipBounds() int getDensity() DrawFilter getDrawFilter() GL getGL() int getHeight() void getMatrix(Matrix ctm) final Matrix getMatrix() int getSaveCount() int getWidth() boolean isOpaque() boolean quickReject(Path path, Canvas.EdgeType type) boolean quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type) boolean quickReject(RectF rect, Canvas.EdgeType type) void restore() void restoreToCount(int saveCount) final void rotate(float degrees, float px, float py) void rotate(float degrees) int save() int save(int saveFlags) int saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags) int saveLayer(RectF bounds, Paint paint, int saveFlags) int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags) int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) final void scale(float sx, float sy, float px, float py) void scale(float sx, float sy) void setBitmap(Bitmap bitmap) void setDensity(int density) void setDrawFilter(DrawFilter filter) void setMatrix(Matrix matrix) void setViewport(int width, int height) void skew(float sx, float sy) void translate(float dx, float dy) 五、android.graphics.Color 有關Android平台上表示顏色的方法有很多種,Color提供了常規主要顏色的定義比如Color.BLACK和Color.GREEN等等,我們平時建立時主要使用以下靜態方法 static int argb(int alpha, int red, int green, int blue) 構造一個包含透明對象的顏色 static int rgb(int red, int green, int blue) 構造一個標準的顏色對象 static int parseColor(String colorString) 解析一種顏色字串的值,比如傳入Color.BLACK 本類返回的均為一個整形類似 綠色為0xff00ff00,紅色為0xffff0000。我們將這個DWORD型看做AARRGGBB,AA代表Aphla透明色,後面的就不難理解,每個分成WORD整好為0-255。 |