Android的用戶界面設計《一》

近期很多網友對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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章