Android自定義view實(shí)現(xiàn)太極效果實(shí)例代碼
之前一直想要個加載的loading。卻不知道用什么好,然后就想到了太極圖標(biāo),最后效果是有了,不過感覺用來做loading簡直丑到爆!!!
實(shí)現(xiàn)效果很簡單,我們不要用什么貝塞爾曲線啥的,因為太極無非就是圓圓圓,只要畫圓就ok了。來上代碼:
因為有黑有白,所以定義2個畫筆分別為黑和白。
private void inital() { whitePaint = new Paint(); whitePaint.setAntiAlias(true); whitePaint.setColor(Color.WHITE); blackPaint = new Paint(); blackPaint.setAntiAlias(true); blackPaint.setColor(Color.BLACK); }最后來畫3個圓就可以解決了:
protected void onDraw(Canvas canvas) { super.onDraw(canvas); Point centerPoint = new Point(width / 2, height / 2); canvas.translate(centerPoint.x, centerPoint.y); canvas.rotate(angle); //繪制兩個半圓 int radius = Math.min(bitmapwidth, bitmapheight) / 2; RectF rect = new RectF(-radius, -radius, radius, radius); //繪制區(qū)域 canvas.drawArc(rect, 90, 180, true, blackPaint); //繪制黑色半圓 canvas.drawArc(rect, -90, 180, true, whitePaint); //繪制白色半圓 //繪制兩個小圓 int smallRadius = radius / 2; canvas.drawCircle(0, -smallRadius, smallRadius, blackPaint); canvas.drawCircle(0, smallRadius, smallRadius, whitePaint); //繪制魚眼 canvas.drawCircle(0, -smallRadius, smallRadius / 4, whitePaint); canvas.drawCircle(0, smallRadius, smallRadius / 4, blackPaint); if (load) { angle += 10; if (angle >= 360) { angle = 0; } } invalidate(); }是不是很簡單,也就幾行代碼就解決了,一開始我還打算用貝塞爾曲線的(瘋了!!)。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選