編輯:高級開發
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。
有關android平台其他的UI和圖形相關內容我們將在今後的9節中講解到,如果有什麼疑問可以來函至[email protected] 我們提供相關的技術支持。便於公開的問題可以直接在我們的論壇中發帖。
android 2.3已經於上周正式發布了。51CTO也已經給大家介紹了android 2.3 SDK的下載地址和如何安裝android 2.3 SDK。具體如何下載安
Next,接受其中的協議,最後會自動安裝。 四、下載 android SDK 下載Android SDK 選擇 android-sdk_r10-Windows.zi
下面介紹下有關android源代碼的內容和編程技巧,但是首先要讓大家了解下什麼是android操作系統,所謂的android系統:基於Linux平台的開源手機操作系統。
前不久我們為大家介紹過在MyEclipse 8.6上搭建android開發環境,本文為一篇外文翻譯,我們將介紹如何學習安裝 android NDK 並開始使用它。在這一