Android之自定義View學習(一) Canvas常用方法: 圖片來源 github地址:https://github.com/SiberiaDante/DrawView ...
Android之自定義View學習(一)
Canvas常用方法:
/** * Created by SiberiaDante on 2017/6/3. */ public class BaseViewDraw extends View { private Paint mPaint1; private Paint mPaint2; private Paint mPaint3; private Paint mPaint4; public BaseViewDraw(Context context) { super(context); } public BaseViewDraw(Context context, AttributeSet attrs) { super(context, attrs); initPaint();//初始化畫筆 } public BaseViewDraw(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private void initPaint() { mPaint1 = new Paint(); mPaint1.setColor(Color.BLUE);//設置畫筆顏色 mPaint1.setStrokeWidth(10f);//設置畫筆寬度 mPaint1.setStyle(Paint.Style.FILL);//設置畫筆填充模式 mPaint2 = new Paint(); mPaint2.setColor(Color.RED); mPaint2.setStrokeWidth(20f); mPaint2.setStyle(Paint.Style.FILL_AND_STROKE); mPaint3 = new Paint(); mPaint3.setColor(Color.BLACK); mPaint3.setStrokeWidth(15f); mPaint3.setStyle(Paint.Style.STROKE); mPaint4 = new Paint(); mPaint4.setColor(Color.GREEN); mPaint4.setStrokeWidth(5f); mPaint4.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 畫顏色 * 瞭解更多canvas前往官方文檔:https://developer.android.com/reference/android/graphics/Canvas.html */ canvas.drawColor(Color.GRAY); /** * 畫點 */ canvas.drawPoint(200, 200, mPaint1);//畫一個點----200,200分別代表在想,x,y軸上的坐標 canvas.drawPoints( new float[]{//畫多個點 300, 300, 300, 400, 300, 500 }, mPaint1); /** * 畫線條 */ canvas.drawLine(10, 10, 200, 500, mPaint1);//10,10代表起點,200,500代表終點 canvas.drawLines(new float[]{//畫多條線段 20, 20, 600, 20, 50, 200, 50, 600}, mPaint1); /** * 畫矩形,三種寫法 * 瞭解更多 Rect前往官方文檔:https://developer.android.com/reference/android/graphics/Rect.html * 瞭解更多 RectF前往官方文檔:http://developer.android.com/reference/android/graphics/RectF.html */ //第一種 canvas.drawRect(500, 100, 800, 400, mPaint1); //第二種 final Rect rect = new Rect(500, 500, 800, 800); canvas.drawRect(rect, mPaint1); //第三種 final RectF rectF = new RectF(500, 900, 800, 1200); canvas.drawRect(rectF, mPaint1); /** * 繪製圓角矩形 */ //方法一 final RectF rectF1 = new RectF(100, 900, 400, 1200); //10,30分別代表圓弧的半徑 canvas.drawRoundRect(rectF1, 10, 30, mPaint2); //方法二 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //這種寫法僅支持API21+ canvas.drawRoundRect(100, 900, 400, 1200, 10, 30, mPaint2); } /** * 畫橢圓 * 350為矩形x軸長度的一半 * 150為矩形y軸長度的一半 */ final RectF rectF2 = new RectF(100, 500, 800, 800); canvas.drawRoundRect(rectF2, 350, 150, mPaint3); /** * 畫圓 * 350為矩形x軸長度的一半 * 350為矩形y軸長度的一半 * 當矩形的長和寬相等時,即為圓 */ //方法一:利用矩形繪製橢圓技巧 final RectF rectF3 = new RectF(100, 500, 800, 1200); canvas.drawRoundRect(rectF3, 350, 350, mPaint3); //方法二:550,800代表圓心位置,400代表圓半徑 canvas.drawCircle(550, 800, 400, mPaint3); /** * 畫圓弧 * 0,90分別代表弧度的起始和結束弧度,順時針為正,區分數學中的逆時針為正 * 第三個Boolean型參數:true為使用中心點,false為不使用中心點,即為一段弧線(不填充狀態下) */ final RectF rectF4 = new RectF(400, 400, 800, 1200); canvas.drawArc(rectF4, 0, 90, true, mPaint4); final RectF rectF5 = new RectF(100, 400, 400, 1200); canvas.drawArc(rectF5, 0, 90, false, mPaint4); } }
github地址:https://github.com/SiberiaDante/DrawView