drawOval
,但是你看这个蛋它明显一头尖一头圆嘛,所以我的思路就是使用drawArc
。drawArc
画半圆,画笔设置为填充drawArc
画椭圆,画笔设置为填充drawArc
方法public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,Paint paint) {
super.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
}
oval
:为确定圆弧区域的矩形,圆弧的中心点为矩形的中心点startAngle
:为圆弧的开始角度(时钟3点的方向为0度,顺时钟方向为正)sweepAngle
:为圆弧的扫过角度(正数为顺时钟方向,负数为逆时钟方向)useCenter
:表示绘制的圆弧是否与中心点连接成闭合区域paint
:paint为绘制圆弧的画笔 /**
* 画笔颜色值
*/
public static final int COLOR_STOCK = Color.parseColor("#F5DEB3");
//抗锯齿
mEggPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//画笔样式填充
mEggPaint.setStyle(Paint.Style.FILL);
mEggPaint.setColor(COLOR_STOCK);
//开启抗锯齿
mEggPaint.setAntiAlias(true);
//开启防抖动
mEggPaint.setDither(true);
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//获取画布的宽度
float canvasWidth = right - left;
//获取画布的高度
float canvasHeight = bottom - top;
//除以二得到中间值
float x = canvasWidth / 2;
float y = canvasHeight / 2;
float r = canvasWidth / 2;
mX = x;
mY = y;
mR = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//上半圆长方形Y方向设置比下半圆长凸显椭圆弧
RectF ovalTop = new RectF(mX-mR, mY-mR-100, mX+mR, mY+mR+100);
//从9点钟方向顺时针画弧线180度未与中心点闭合,画笔模式为填充
canvas.drawArc(ovalTop, 180, 180, false, mEggPaint);
//下半圆长方形半圆弧
RectF oval3 = new RectF(mX-mR, mY-mR, mX+mR, mY+mR);
//从3点钟方向顺时针画弧线180度未与中心点闭合,画笔模式为填充
canvas.drawArc(oval3, 0, 180, false, mEggPaint);
}
100dp
和200dp
毕竟蛋
是长方形嘛。
效果图
注意如果是四个半圆画蛋的话第四个参数要设置为true,否则你画蛋中间将是空心的