在Android開發中常常會遇到特殊的需求要求開發者實現獨特的視圖UI。因此對於開發者來講自定義View能夠實現多種多樣的需求效果。那此次咱們就來講說關於自定義View的實現。android
View的繪製是在方法onDraw中實現的。經過獲取View的畫布canvas,使用Panit在canvas上繪製內容。canvas
public class CustomerView extends View { @Override protected void onDraw(Canvas canvas) { } }
就和咱們實際畫畫同樣,繪製須要一支畫筆才行。Paint就是咱們繪製所須要的畫筆。實例化Paint並設置繪製所須要的參數就能繪製出本身所須要的內容。Android開發文檔中Paint實現方法不少,這裏咱們介紹幾個經常使用基本的方法。ide
setAlpha(bookean a) //設置抗鋸齒 setColor(int color) //設置畫筆顏色 setShader(Shader shader)//設置畫筆漸變顏色 setStyle(Paint.Style style)//設置畫筆模式(例如實心/空心) setXfermode(Xfermode xfermode)//設置畫筆繪製模式 setStrokeWidth(float width) //設置畫筆粗細
有了畫筆,如今就是要用畫筆在畫布上畫畫了。想要繪製怎麼樣的圖形是經過Canvas的方法實現的。咱們也介紹幾個經常使用基本的繪製方法。只要理科學的好人人都是現代派畫家O(∩_∩)O~。學習
drawARGB(int a, int r, int g, int b)//設置畫布背景顏色 drawColor(int color)//設置畫布背景顏色 drawRect(float left, float top, float right, float bottom, Paint paint)//繪製矩形 drawPath(Path path, Paint paint)//繪製自定義圖形 drawCircle(float cx, float cy, float radius, Paint paint)//繪製原型 drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)//繪製弧以及圓 drawLine(float startX, float startY, float stopX, float stopY, Paint paint)//繪製線條 drawText(String text, int start, int end, float x, float y, Paint paint)//繪製文字 drawPoint(float x, float y, Paint paint)//繪製點 drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)//繪製圓角矩形 drawOval(RectF oval, Paint paint)//繪製圓 drawBitmap(Bitmap bitmap, float left, float top, Paint paint) //繪製圖片
知道了畫筆Paint和畫布Canavs的基本方法,最後咱們實踐畫一畫爲成爲畫家邁出第一步。this
public class NormalView extends BaseView { Path triangle2; Path all; private float[] mTickRightStartPosition = new float[2]; public NormalView(Context context) { super(context); } public NormalView(Context context, AttributeSet attrs) { super(context, attrs); } public NormalView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } void initView(){ mPaint = new Paint(); mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(5); mPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mCanVas = canvas; //繪製正方形 mCanVas.drawRect(20,40,60,80,mPaint); //繪製矩形 mCanVas.drawRect(20,100,60,160,mPaint); //繪製三角形 Path path = new Path(); path.moveTo(60, 200);// 此點爲多邊形的起點 path.lineTo(40, 250); path.lineTo(80, 250); path.close(); // 使這些點構成封閉的多邊形 mCanVas.drawPath(path,mPaint); //畫圓 mCanVas.drawCircle(40,400,40,mPaint); //畫文字 mPaint.setTextSize(50); mCanVas.drawText("Hello world!",0,12,40,500,mPaint); //畫圖片 Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher); mCanVas.drawBitmap(bitmap,40,600,mPaint); //畫背景色 mCanVas.drawARGB(100,100,100,100); //畫弧以及畫橢圓 RectF rectF = new RectF(); Rect rect = new Rect(40,750,150,800); rectF.set(rect); mCanVas.drawArc(rectF,0,-180,true,mPaint); //畫線 mCanVas.drawLine(40,850,100,900,mPaint); //畫點 mCanVas.drawPoint(40,950,mPaint); //畫圓角矩形 RectF roundRectF = new RectF(); Rect roundRect = new Rect(40,1000,400,1050); roundRectF.set(roundRect); mCanVas.drawRoundRect(roundRectF,20,5,mPaint); //畫圓 RectF OvalRectF = new RectF(); Rect OvalRect = new Rect(40,1100,60,1200); OvalRectF.set(OvalRect); mCanVas.drawOval(OvalRectF,mPaint); Path hexagon = new Path(); hexagon.moveTo(40,1400); hexagon.lineTo(90,1350); hexagon.lineTo(140,1350); hexagon.lineTo(190,1400); hexagon.lineTo(140,1450); hexagon.lineTo(90,1450); hexagon.lineTo(40,1400); hexagon.close(); mCanVas.drawPath(hexagon,mPaint); Path triangle1 = new Path(); triangle1.moveTo(500,100); triangle1.lineTo(500,300); triangle1.lineTo(700,300); triangle1.lineTo(500,100); triangle1.close(); triangle2 = new Path(); triangle2.moveTo(700,100); triangle2.lineTo(900,100); triangle2.lineTo(900,300); triangle2.lineTo(700,100); triangle2.close(); all = new Path(); all.addPath(triangle1); all.addPath(triangle2); mCanVas.drawPath(all,mPaint); } }
實踐成功!咱們接下來還要繼續努力,成爲畫家這仍是剛開始呢。下一次咱們繼續學習關於View的內容。Fighting!spa