首先簡單的一個畫矩形的例子java
public class DrawView extends View { public DrawView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint p = new Paint(); Rect t = new Rect(1,1,100,100); canvas.drawRect(t.left, t.top, t.right, t.bottom, p); } }
畫布, 用於繪製相關的圖形
Canvas能夠繪製的對象有:canvas
弧線(arcs) canvas.
填充顏色(argb和color)
圖 Bitmap
圓(circle和oval)
點(point)
線(line)
矩形(Rect)
圖片(Picture)
圓角矩形 (RoundRect)
文本(text)
頂點(Vertices)
路徑(path)ide
Canvas的基本操做有:字體
canvas.save():把當前的繪製的圖像保存起來,讓後續的操做至關因而在一個新的圖層上的操做。
canvas.restore(); 把當前畫布返回(調整)到上一個save()狀態以前
canvas.translate(dx, dy); //把當前畫布的原點移到(dx,dy),後面的操做都以(dx,dy)做爲參照點,默認原點爲(0,0)優化
canvas.scale(x,y);擴大。x爲水平方向的放大倍數,y爲豎直方向的放大倍數
canvas.rotate(angel):旋轉.angle指旋轉的角度,順時針旋轉。
canvas.transform():切變。所謂切變,其實就是把圖像的頂部或底部推到一邊。
canvas.saveLayer(bounds, paint, saveFlags);動畫
示例rest
protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint mPaint = new Paint(); // 繪製畫布背景 canvas.drawColor(Color.GRAY); //設置畫筆顏色 mPaint.setColor(Color.BLUE); //設置畫筆爲空心 若是將這裏改成Style.STROKE 這個圖中的實線圓柱體就變成了空心的圓柱體 mPaint.setStyle(Paint.Style.STROKE); //繪製直線 canvas.drawLine(50, 50, 450, 50, mPaint); //繪製矩形 canvas.drawRect(100, 100, 200, 300, mPaint); //繪製矩形 mPaint.setStyle(Paint.Style.FILL); canvas.drawRect(300, 100, 400, 400, mPaint); mPaint.setColor(Color.YELLOW); RectF r = new RectF(150, 500, 270, 600); // 畫矩形 canvas.drawRect(r, mPaint); // 畫圓 canvas.drawCircle(50, 500, 50, mPaint); RectF oval = new RectF(350, 500, 450, 700); // 畫橢圓 canvas.drawOval(oval, mPaint); RectF rect = new RectF(100, 700, 170, 800); // 畫圓角矩形 canvas.drawRoundRect(rect, 30, 20, mPaint); //繪製圓弧 繪製弧形 mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.RED); RectF re1 = new RectF(1000, 50, 1400, 200); canvas.drawArc(re1, 10, 270, false, mPaint); RectF re2 = new RectF(1000, 300, 1400, 500); canvas.drawArc(re2, 10, 270, true, mPaint); //設置Path路徑 mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.GREEN); mPaint.setStrokeWidth(3); Path path = new Path(); path.moveTo(500, 100); path.lineTo(920, 80); path.lineTo(720, 200); path.lineTo(600, 400); path.close(); mPaint.setTextSize(46); canvas.drawPath(path, mPaint); canvas.drawTextOnPath("***TEST**", path, -20, -20, mPaint); //三角形 path.moveTo(10, 330); path.lineTo(70, 330); path.lineTo(40, 270); path.close(); canvas.drawPath(path, mPaint); // 畫三角形 canvas.save(); }
繪製筆,繪製圖形時的屬性
基本用法code
Paint paint = new Paint(); // 設置字體顏色 paint.setColor(Color.RED); // 防鋸齒 paint.setAntiAlias(true); //設置顏色過濾器,能夠在繪製顏色時實現不用顏色的變換效果 paint.setColorFilter(ColorFilter); //若是該項設置爲true,則圖像在動畫進行中會濾掉對Bitmap圖像的優化操做,加快顯示 //速度,本設置項依賴於dither和xfermode的設置 paint.setFilterBitmap(true); //當畫筆樣式爲STROKE或FILL_OR_STROKE時,設置筆刷的粗細度 paint.setStrokeWidth(10f); //設置繪製路徑的效果,如點畫線等 paint.setPathEffect(PathEffect); //設置圖像效果,使用Shader能夠繪製出各類漸變效果 // Shader.TileMode三種模式 // REPEAT:沿着漸變方向循環重複 // CLAMP:若是在預先定義的範圍外畫的話,就重複邊界的顏色 // MIRROR:與REPEAT同樣都是循環重複,但這個會對稱重複 Shader shader = new LinearGradient(0, 0, 100, 100, new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW}, null, Shader.TileMode.REPEAT); paint.setShader(shader); //設置MaskFilter,能夠用不一樣的MaskFilter實現濾鏡的效果,如濾化,立體等 paint.setMaskFilter(MaskFilter); //在圖形下面設置陰影層,產生陰影效果,radius爲陰影的角度,dx和dy爲陰影在x軸和y軸上的距離,color爲陰影的顏色 paint.setShadowLayer(float radius, float dx, float dy, int color); //設置畫筆的樣式,爲FILL,FILL_OR_STROKE,或STROKE paint.setStyle(Paint.Style); //當畫筆樣式爲STROKE或FILL_OR_STROKE時,設置筆刷的圖形樣式,圓形樣式ROUND,或方形樣式SQUARE BUTT paint.setStrokeCap(Paint.Cap); //設置繪製時畫筆與圖形的結合方式,METER\ROUND\BEVEL 平滑效果 paint.setSrokeJoin(Paint.Join); // 字體下劃線 paint.setUnderlineText(true); // 暫時不知,有清楚的能夠告訴我,謝謝 paint.setLinearText(true); // 字體加粗 paint.setFakeBoldText(true); // 防抖動 paint.setDither(true); // 透明度 paint.setAlpha(0xF0);