天天看點

android:繪圖 (android.graphics包)

android:繪圖

View:元件,了解為畫布

Drawable:所有可見對象的描述,了解為:素材類

Bitmap:圖檔類

Canvas:畫筆

Paint:畫筆樣式與顔色、特效的集合

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

今天我們繼續介紹Android平台底層繪圖類的相關内容,在Android UI開發專題(一) 之界面設計中我們介紹了有關Android平台資源使用以及Bitmap相關類的操作,接下來将會以執行個體的方式給大家示範各種類的用處以及注意點。今天我們繼續了解android.graphics包中比較重要的繪圖類。 

  一、 android.graphics.Matrix

  有關圖形的變換、縮放等相關操作常用的方法有:

  void  reset() // 重置一個matrix對象。

  void  set(Matrix src) //複制一個源矩陣,和本類的構造方法 Matrix(Matrix src)  一樣

  boolean  isIdentity() //傳回這個矩陣是否定義(已經有意義)

  void setRotate(float degrees) //指定一個角度以0,0為坐标進行旋轉

  void  setRotate(float degrees, float px, float py)  //指定一個角度以px,py為坐标進行旋轉

  void  setScale(float sx, float sy)  // 縮放

  void  setScale(float sx, float sy, float px, float py)  //以坐标px,py進行縮放

  void setTranslate(float dx, float dy) //平移

void setSkew (float kx, float ky, float px, float py) //以坐标px,py進行傾斜

void setSkew (float kx, float ky) //傾斜

  二、android.graphics.NinePatch

  NinePatch是Android平台特有的一種非矢量圖形自然拉伸處理方法,可以幫助正常的圖形在拉伸時不會縮放,執行個體中Android開發網提示大家對于Toast的顯示就是該原理,同時SDK中提供了一個工具名為Draw 9-Patch,有關該工具的使用方法可以參考我們經釋出的 Draw 9-Patch使用方法介紹一文。由于該類提供了高品質支援透明的縮放方式,是以圖形格式為PNG,檔案命名方式為.9.png 的字尾比如android123.9.png。

  三、android.graphics.Paint

  Paint類我們可以了解為畫筆、畫刷的屬性定義,本類常用的方法如下:

  void  reset()  //重置

  void  setARGB(int a, int r, int g, int b) 或 void  setColor(int color) 均為設定Paint對象的顔色

void  setAntiAlias(boolean aa)  //是否抗鋸齒,需要配合void setFlags (Paint.ANTI_ALIAS_FLAG) 來幫助消除鋸齒使其邊緣更平滑。

Shader  setShader(Shader shader) //設定陰影,Shader類是一個矩陣對象,如果為NULL将清除陰影。

void  setStyle(Paint.Style style)  //設定樣式,一般為 FILL 填充,或者STROKE凹陷效果。

void  setTextSize(float textSize)  //設定字型大小

void  setTextAlign(Paint.Align align) //文本對齊方式

Typeface  setTypeface(Typeface typeface)  //設定字型,通過Typeface可以加載Android内部的字型,一般為宋體對于中文,部分ROM可以自己添加比如雅黑等等

void  setUnderlineText(boolean underlineText) //是否設定下劃線,需要撇和void setFlags (Paint.UNDERLINE_TEXT_FLAG) 方法。

四、android.graphics.Rect

  Rect我們可以了解為矩形區域,類似的還有Point一個點,Rect類除了表示一個矩形區域位置描述外,android123提示主要可以幫助我們計算圖形之間是否碰撞(包含)關系,對于Android遊戲開發比較有用,其主要的成員contains包含了三種重載方法,來判斷包含關系

boolean  contains(int left, int top, int right, int bottom) 

boolean  contains(int x, int y) 

boolean  contains(Rect r)

五、android.graphics.Region

Region在Android平台中表示一個區域和Rect不同的是,它表示的是一個不規則的樣子,可以是橢圓、多邊形等等,而Rect僅僅是矩形。同樣Region的boolean  contains(int x, int y)  成員可以判斷一個點是否在該區域内

六、android.graphics.Typeface

  Typeface類是幫助描述一個字型對象,在TextView中通過使用setTypeface方法來制定一個輸出文本的字型,其直接構造調用成員create方法可以直接指定一個字型名稱和樣式,比如

  static Typeface  create(Typeface family, int style) 

  static Typeface  create(String familyName, int style)

  同時使用isBold和isItalic方法可以判斷出是否包含粗體或斜體的字型。

  final boolean  isBold() 

  final boolean  isItalic()

  該類的建立方法還有從apk的資源或從一個具體的檔案路徑,其具體方法為

  static Typeface  createFromAsset(AssetManager mgr, String path) 

  static Typeface  createFromFile(File path) 

  static Typeface  createFromFile(String path)

      本文轉自xyz_lmn51CTO部落格,原文連結:http://blog.51cto.com/xyzlmn/816808,如需轉載請自行聯系原作者

繼續閱讀