GitHub 地址：https://github.com/leavesC/CustomView

• 小圆半径从 0 逐渐增大到 minRadius ，此过程只是半径增大，其坐标点不变，依然处于最左边
• 小圆从左边逐渐位移到中间位置，此过程半径逐渐从 minRadius 增大到 maxRadius，X 坐标逐渐增大，Y 坐标不变
• 小圆从中间逐渐位移到最右边，此过程半径从 maxRadius 逐渐减小到 minRadius，X 坐标逐渐增大，Y 坐标不变
• 小圆处于最右边保持坐标点不变，半径逐渐从 minRadius 减小到 0

    private void updateCircle(int index, float fraction) {
//             x              x               x
// ------------||-------------||--------------||------------
//            1/4            2/4             3/4
//      1/4            2/4            3/4            4/4

//   左边-绿色
// 半径从0到min  半径从min到max    半径从max到min   半径从min到0

//   中间-橙色
//                                半径从max到min   半径从min到0
//半径从0到min   半径从min到max

//   右边-红色
//                                                 半径从min到0
//半径从0到min  半径从min到max     半径从max到min

float radius = 0;
float x = 0;
switch (index) {
case LEFT: {
if (fraction <= 1f / 4f) {
radius = minRadius * (4f * fraction);
x = minRadius;
} else if (fraction <= 0.5f) {
float percent = (fraction - 1f / 4f) * 4f;
radius = minRadius + percent * (maxRadius - minRadius);
x = minRadius + percent * (contentWidth / 2f - minRadius);
} else if (fraction <= 3f / 4f) {
float percent = (fraction - 0.5f) * 4f;
radius = maxRadius - percent * (maxRadius - minRadius);
x = contentWidth / 2f + percent * (contentWidth / 2f - minRadius);
} else {
radius = minRadius - (fraction - 3f / 4f) * 4f * minRadius;
x = contentWidth - minRadius;
}
break;
}
case CENTER: {
if (fraction <= 1f / 4f) {
float percent = fraction * 4f;
radius = maxRadius - (maxRadius - minRadius) * percent;
x = contentWidth / 2f + (contentWidth / 2f - minRadius) * percent;
} else if (fraction <= 0.5f) {
radius = minRadius - (fraction - 1f / 4f) * 4f * minRadius;
x = contentWidth - minRadius;
} else if (fraction <= 3f / 4f) {
radius = minRadius * (4f * (fraction - 0.5f));
x = minRadius;
} else {
float percent = (fraction - 3f / 4f) * 4f;
radius = minRadius + (maxRadius - minRadius) * percent;
x = minRadius + (contentWidth / 2f - minRadius) * percent;
}
break;
}
case RIGHT: {
if (fraction <= 1f / 4f) {
radius = minRadius - 4f * fraction * minRadius;
x = contentWidth - minRadius;
} else if (fraction <= 0.5f) {
radius = minRadius * (4f * (fraction - 1f / 4f));
x = minRadius;
} else if (fraction <= 3f / 4f) {
float percent = (fraction - 0.5f) * 4f;
radius = minRadius + (maxRadius - minRadius) * percent;
x = minRadius + (contentWidth / 2f - minRadius) * percent;
} else {
float percent = (fraction - 3f / 4f) * 4f;
radius = maxRadius - (maxRadius - minRadius) * percent;
x = contentWidth / 2f + (contentWidth / 2f - minRadius) * percent;
}
break;
}
}
Circle circle = circleList.get(index);
circle.radius = radius;
circle.x = x;
}


protected void onDraw(Canvas canvas) {
for (Circle circle : circleList) {
paint.setColor(circle.color);
canvas.drawCircle(circle.x + getPaddingLeft(), circle.y + getPaddingTop(), circle.radius, paint);
}
}
© 著作权归作者所有

0条评论