Android自定义view实现太极效果实例代码
之前一直想要个加载的loading。却不知道用什么好,然后就想到了太极图标,最后效果是有了,不过感觉用来做loading简直丑到爆!!!
实现效果很简单,我们不要用什么贝塞尔曲线啥的,因为太极无非就是圆圆圆,只要画圆就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); //绘制区域 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(); }
是不是很简单,也就几行代码就解决了,一开始我还打算用贝塞尔曲线的(疯了!!)。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!