Android View之心形

(一)最优因子

1
2
3
4
5
6
7

对于有n段的贝塞尔曲线来说,到控制点的最优距离(因子)是(4/3)*tan(π/(2n))。

圆从坐标系来看,由4段圆弧组成,同时对应4个(数据)点。
所以圆的数据点到控制点的最优距离因子为 DIS_FACTOR = (4/3)*tan(π/8),
由三角形倍角公式:tana/2 = sina/(1+cosa);可得tanπ/8 = √2 - 1;
即 DIS_FACTOR = (4/3)*(√2 - 1) = 0.5522847498307933984022516...

(二)心形的实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* 自定义View -- 心形(由4段三阶贝塞尔曲线组成)
*
* @author [*昨日重现*] lhy_ycu@163.com
* @since version 1.0
*/
public class HeartView extends View {
// 数据点到达控制点的最佳距离因子
private static final double DIST_FACTOR = 0.552284749831;
// 心形变化次数
private static final float mTotalCount = 10;

private Paint mPaint;
// "心形"曲线
private Path mHeartPath;

// 中心点坐标
private int centerX;
private int centerY;
// 圆的半径(可由参数传入)
private float mRadius;
// 数据点与控制之间的差值
private float mDifference;
// 当前执行次数
private float mCurrentCount;

// 4个数据点
private PointF[] mDataPoint = new PointF[4];
// 8个控制点
private PointF[] mControlPoint = new PointF[8];

private Context mContext;
private boolean stopFlag = false;

public HeartView(Context context) {
this(context, null);
}

public HeartView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
}

private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mHeartPath = new Path();

mRadius = DensityUtils.dip2px(mContext, 100);
mDifference = (float) (mRadius * DIST_FACTOR);

// 初始化数据点
// A
mDataPoint[0] = new PointF(mRadius, 0);
// B
mDataPoint[1] = new PointF(0, -mRadius);
// C
mDataPoint[2] = new PointF(-mRadius, 0);
// D
mDataPoint[3] = new PointF(0, mRadius);

// 初始化控制点(从最右边逆时针起,开始描点)
// A1、A2
mControlPoint[0] = new PointF(mDataPoint[0].x, mDataPoint[0].y - mDifference);
mControlPoint[1] = new PointF(mDataPoint[1].x + mDifference, mDataPoint[1].y);
// B1、B2
mControlPoint[2] = new PointF(mDataPoint[1].x - mDifference, mDataPoint[1].y);
mControlPoint[3] = new PointF(mDataPoint[2].x, mDataPoint[2].y - mDifference);
// C1、C2
mControlPoint[4] = new PointF(mDataPoint[2].x, mDataPoint[2].y + mDifference);
mControlPoint[5] = new PointF(mDataPoint[3].x - mDifference, mDataPoint[3].y);
// D1、D2
mControlPoint[6] = new PointF(mDataPoint[3].x + mDifference, mDataPoint[3].y);
mControlPoint[7] = new PointF(mDataPoint[0].x, mDataPoint[0].y + mDifference);
}

// 2、测量(控制)View的大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = measureDimension(DensityUtils.dip2px(mContext, 250), widthMeasureSpec);
int height = measureDimension(DensityUtils.dip2px(mContext, 250), heightMeasureSpec);
setMeasuredDimension(width, height);// 保存最终测量的宽高
}

// 注意:这里defaultSize参数可由自定义属性传入构造方法拿到,这里暂时写死。
private int measureDimension(int defaultSize, int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;// 指定大小
} else {
result = defaultSize;// 默认大小
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.centerX = w / 2;
this.centerY = h / 2;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
DebugLog.e("绘制开始-------1111----");
if (stopFlag) return;
// 将坐标系移动到画布中央
canvas.translate(centerX, centerY);
// 绘制背景色
canvas.drawColor(Color.CYAN);

// 绘制坐标系
drawCoordinateSystem(canvas);
// 描点
drawPoints(canvas);
// 绘制顶点(A、B、C、D)文本
drawPointText(canvas);
// 绘制数据点与控制点进行连线
drawLines(canvas);
// 绘制心形曲线
drawHeartCurve(canvas);

mCurrentCount++;
if (mCurrentCount < mTotalCount) {
// 最上面1个数据点上下移
mDataPoint[1].y += 200 / mTotalCount;
// 最下面2个控制点往上移
mControlPoint[5].y -= 160 / mTotalCount;
mControlPoint[6].y -= 160 / mTotalCount;
// 最下面旁边2个控制点往内移
mControlPoint[4].x += 80 / mTotalCount;
mControlPoint[7].x -= 80 / mTotalCount;
postInvalidateDelayed(100L);
} else {
// 重置关键点坐标
mCurrentCount = 0;
mDataPoint[1].y = -mRadius;
mControlPoint[5].y = mDataPoint[3].y;
mControlPoint[6].y = mDataPoint[3].y;
mControlPoint[4].x = mDataPoint[2].x;
mControlPoint[7].x = mDataPoint[0].x;
postInvalidateDelayed(1000L);
}

DebugLog.e("绘制结束-------222----");
}

private void drawCoordinateSystem(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(3F);
mPaint.setColor(Color.argb(127, 0, 0, 0));
canvas.drawLine(-centerX, 0, centerX, 0, mPaint);
canvas.drawLine(0, -centerY, 0, centerY, mPaint);
}

private void drawPoints(Canvas canvas) {
mPaint.setStrokeWidth(20F);
mPaint.setColor(Color.BLACK);
canvas.drawPoint(0, 0, mPaint);
// 绘制数据点
for (PointF pointF : mDataPoint) {
canvas.drawPoint(pointF.x, pointF.y, mPaint);// A、B、C、D
}
mPaint.setColor(Color.BLUE);
// 绘制控制点
for (PointF pointF : mControlPoint) {
canvas.drawPoint(pointF.x, pointF.y, mPaint);
}
}

private void drawPointText(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(2F);
mPaint.setColor(Color.RED);
mPaint.setTextSize(DensityUtils.sp2px(mContext, 8F));
float fontH = DensityUtils.getFontHeight(mPaint);
char[] peakChar = "ABCD".toCharArray();
for (int i = 0; i < mDataPoint.length; i++) {
if (i == 1) {
canvas.drawText(peakChar, i, 1, mDataPoint[i].x, mDataPoint[i].y - fontH / 2, mPaint);
} else if (i == 2) {
canvas.drawText(peakChar, i, 1, mDataPoint[i].x - mPaint.measureText(String.valueOf(peakChar[i])), mDataPoint[i].y + fontH, mPaint);
} else {
canvas.drawText(peakChar, i, 1, mDataPoint[i].x, mDataPoint[i].y + fontH, mPaint);
}
}
}

private void drawLines(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5F);
mPaint.setColor(Color.GRAY);
// A
canvas.drawLine(mDataPoint[0].x, mDataPoint[0].y, mControlPoint[0].x, mControlPoint[0].y, mPaint);
canvas.drawLine(mDataPoint[0].x, mDataPoint[0].y, mControlPoint[7].x, mControlPoint[7].y, mPaint);
// B
canvas.drawLine(mDataPoint[1].x, mDataPoint[1].y, mControlPoint[1].x, mControlPoint[1].y, mPaint);
canvas.drawLine(mDataPoint[1].x, mDataPoint[1].y, mControlPoint[2].x, mControlPoint[2].y, mPaint);
// C
canvas.drawLine(mDataPoint[2].x, mDataPoint[2].y, mControlPoint[3].x, mControlPoint[3].y, mPaint);
canvas.drawLine(mDataPoint[2].x, mDataPoint[2].y, mControlPoint[4].x, mControlPoint[4].y, mPaint);
// D
canvas.drawLine(mDataPoint[3].x, mDataPoint[3].y, mControlPoint[5].x, mControlPoint[5].y, mPaint);
canvas.drawLine(mDataPoint[3].x, mDataPoint[3].y, mControlPoint[6].x, mControlPoint[6].y, mPaint);
}

private void drawHeartCurve(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.argb(225, 255, 0, 0));
mHeartPath.reset();
mHeartPath.moveTo(mDataPoint[0].x, mDataPoint[0].y);
mHeartPath.cubicTo(mControlPoint[0].x, mControlPoint[0].y, mControlPoint[1].x, mControlPoint[1].y, mDataPoint[1].x, mDataPoint[1].y);
mHeartPath.cubicTo(mControlPoint[2].x, mControlPoint[2].y, mControlPoint[3].x, mControlPoint[3].y, mDataPoint[2].x, mDataPoint[2].y);
mHeartPath.cubicTo(mControlPoint[4].x, mControlPoint[4].y, mControlPoint[5].x, mControlPoint[5].y, mDataPoint[3].x, mDataPoint[3].y);
mHeartPath.cubicTo(mControlPoint[6].x, mControlPoint[6].y, mControlPoint[7].x, mControlPoint[7].y, mDataPoint[0].x, mDataPoint[0].y);
canvas.drawPath(mHeartPath, mPaint);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
stopFlag = true;
}

}
Hawky wechat
欢迎订阅我的微信公众号
坚持原创技术分享,您的支持将鼓励我继续创作!

分享