Android的繪圖應該繼承View組件,並重寫它的onDraw(Canvas canvas)方法便可。android
重寫onDraw(Canvas canvas)方法時涉及一個繪圖API:Canvas,Canvas表明了「依附」於指定View的畫布,它提供瞭如表7.1所示的方法繪製各類圖形。canvas
方法簽名 | 簡要說明 |
---|---|
drawArc(RecF oval,float startAngle,flaot sweepAngle,boolean useCenter,Paint paint) | 繪製弧度 |
drawBitmap(Bitmap bitmap,Rect src,Rect dst,Paint paint) | 在指定點繪製從源位圖中「挖取」的一塊 |
drawBitmap(Bitmap bitmap,float left,float top,Paint paint) | 在指定點繪製位圖 |
drawCircle(float cx,float cy,float radius,Paint paint) | 在指定點繪製一個圓形 |
drawLine(float startX,float startY,float stopX,float stopY,Paint paint) | 繪製一條線 |
drawLines(float[]pts,int offset,int count,Paint paint) | 繪製多條線 |
drawOval(RectF oval,Paint paint) | 繪製橢圓 |
drawPath(Path path,Paint paint) | 沿着指定path繪製任意形狀 |
drawPoint(float x,float y,Paint) | 繪製一個點 |
drawPoints(float[] pts,int offset,int count,Paint paint) | 繪製多個點 |
drawRec(float left,float top,float right,float bottom,Paint paint) | 繪製矩形 |
drawRoundRect(RectF rect,float rx,float ry,Paint paint) | 繪製圓角矩形 |
drawText(String text,int start,int end,Paint paint) | 繪製字符串 |
drawTextOnPath(String text,Path path,float hOffset,float vOffset,Paint paint) | 沿着路徑繪製字符串 |
clipRect(float left,float top,float right,float bottom) | 剪切一個矩形區域 |
clipRegion(Region region) | 剪切指定區域 |
除了表7.1所定義的各類方法以外,Canvas還提供了以下方法進行座標變換。ide
Canvas提供的上面的方法還涉及一個API:Paint,Paint表明了Canvas上的畫筆,所以Paint類主要用於設置繪製風格,包括畫筆顏色、畫筆筆觸粗細、填充風格等。Paint提供瞭如表7.2所示的方法。spa
方法簽名 | 簡要說明 |
---|---|
setARGB(int a,int g,int b)/setColor(int color) | 設置顏色 |
setAlpha(int a) | 設置透明度 |
setAntiAlias(boolean aa) | 設置是否抗鋸齒 |
setColor(int color) | 設置顏色 |
setPathEffect(PathEffect effect) | 設置繪製路徑時的路徑效果 |
setShader(Shader shader) | 設置畫筆的填充效果 |
setShadowLayer(float radius,float dx,float dy,int color) | 設置陰影 |
setStrokeWidth(float width) | 設置畫筆的筆觸寬度 |
setStrokeJoin(Paint.Join join) | 設置畫筆轉彎處的鏈接風格 |
setStyle(Paint.Style style) | 設置Paint的填充風格 |
setTextAlign(Paint.Align align) | 設置繪製文本時文本的對齊方式 |
setTextSize(float textSize) | 設置繪製文本時的文字大小 |
在Canvas提供的繪製方法中還用到了一個API:Path,Path表明了任意多條直線鏈接而成的任意圖形,當Canvas根據Path繪製時,它能夠繪製出任意的形狀。code
下面的程序示範瞭如何在Android應用中繪製基本的集合圖形,該程序的關鍵在於一個自定義的View組件,程序重寫該View組件的onDraw(Canvas)方法,接下來在該Canvas上繪製了大量幾何圖形。這個自定義View的代碼以下。對象
package com.example.studydrawable; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Shader; import android.util.AttributeSet; import android.view.View; public class MyView extends View { public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override //重寫該方法,進行繪圖 protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //把整張畫布繪製成白色 canvas.drawColor(Color.WHITE); Paint paint=new Paint(); //去鋸齒 paint.setAntiAlias(true); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); //繪製圓形 canvas.drawCircle(40, 40, 30, paint); //繪製正方形 canvas.drawRect(10, 80,70,140,paint); //繪製矩形 canvas.drawRect(10, 150,70,190,paint); RectF re1=new RectF(10,200,70,230); //繪製圓角矩形 canvas.drawRoundRect(re1, 15, 15, paint); RectF rel1=new RectF(10,240,70,270); //繪製橢圓 canvas.drawOval(rel1, paint); //定義一個Path對象,封閉成一個三角形 Path path1=new Path(); path1.moveTo(10, 340); path1.lineTo(70, 340); path1.lineTo(40, 290); path1.close(); //根據Path進行繪製,繪製三角形 canvas.drawPath(path1,paint); //定義一個Path對象,封閉成一個五角形 Path path2=new Path(); path2.moveTo(26, 360); path2.lineTo(54, 360); path2.lineTo(70, 392); path2.lineTo(40, 420); path2.lineTo(10, 392); path2.close(); //根據path進行繪製,繪製五角形 canvas.drawPath(path2, paint); //----------設置填充風格後繪製---------------- paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); canvas.drawCircle(120, 40, 30, paint); //繪製正方形 canvas.drawRect(90, 80,150,140,paint); //繪製矩形 canvas.drawRect(90, 150,150,190,paint); RectF re2=new RectF(90,200,150,230); //繪製圓角矩形 canvas.drawRoundRect(re2, 15, 15,paint); RectF re21=new RectF(90,240,150,270); //繪製橢圓 canvas.drawOval(re21, paint); Path path3=new Path(); path3.moveTo(90, 340); path3.lineTo(150, 340); path3.lineTo(120, 290); path3.close(); //繪製三角形 canvas.drawPath(path3, paint); Path path4=new Path(); path4.moveTo(106, 360); path4.lineTo(134, 360); path4.lineTo(150, 392); path4.lineTo(120, 420); path4.lineTo(90, 392); path4.close(); //繪製五角形 canvas.drawPath(path4, paint); //---------設置漸變器後繪製---------- //爲Paint設置漸變器 Shader mShader=new LinearGradient(0,0,40,60,new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW} ,null,Shader.TileMode.REPEAT); paint.setShader(mShader); //設置陰影 paint.setShadowLayer(45,10, 10, Color.GRAY); //繪製圓形 canvas.drawCircle(200, 40, 30, paint); //繪製正方形 canvas.drawRect(170,80,230,140, paint); //繪製矩形 canvas.drawRect(170,150,230,190, paint); RectF re3=new RectF(170,200,230,230); //繪製圓角矩形 canvas.drawRoundRect(re3, 15, 15, paint); RectF re31=new RectF(170,240,230,270); //繪製橢圓 canvas.drawOval(re31, paint); Path path5=new Path(); path5.moveTo(170, 340); path5.lineTo(230, 340); path5.lineTo(200, 290); path5.close(); //根據Path進行繪製,繪製三角形 canvas.drawPath(path5, paint); Path path6=new Path(); path6.moveTo(186, 360); path6.lineTo(214, 360); path6.lineTo(230, 392); path6.lineTo(200, 420); path6.lineTo(170, 392); path6.close(); //根據Path進行繪製,繪製五角形 canvas.drawPath(path6,paint); //------設置字符大小後繪製-------- paint.setTextSize(24); paint.setShader(null); //繪製7個字符串 canvas.drawText(getResources().getString(R.string.circle), 240, 50, paint); canvas.drawText(getResources().getString(R.string.square), 240, 120, paint); canvas.drawText(getResources().getString(R.string.rect), 240, 175, paint); canvas.drawText(getResources().getString(R.string.round_rect), 230, 220, paint); canvas.drawText(getResources().getString(R.string.oval), 240, 260, paint); canvas.drawText(getResources().getString(R.string.triangle), 240, 325, paint); canvas.drawText(getResources().getString(R.string.circle), 240, 390, paint); } }
上面的程序中大量調用了Canvas的方法來繪製幾何圖形,並且程序的粗體字代碼還爲Paint畫筆設置了使用漸變、陰影,所以接下來繪製的幾何圖形將採用漸變填充,並且具備陰影。使用一個Activity來顯示上blog