直接上代码RoundRectLine.java
public class RoundRectLine {
private RectF roundRect = new RectF();
private int imgWidth;
private int imgHeight;
private int lineWidth;
private float rx;
private float ry;
private Paint pen = new Paint();
private RectF[] angleoval = new RectF[4];
public RoundRectLine() {
for(int i = 0 ; i <angleoval.length ; i++){
angleoval[i] = new RectF();
}
}
public void setRoundRectLine(int left, int top, int width, int height,
float rx, float ry, int lineWidth) {
this.rx = rx;
this.ry = ry;
this.lineWidth = lineWidth;
roundRect.set(left, top, left + width, top + height);
imgWidth = width + 2 * lineWidth;
imgHeight = height + 2 * lineWidth;
pen.setStrokeWidth(lineWidth);
angleoval[0].set(left + width - 2 * rx, top, left + width, top + 2 * ry);
angleoval[1].set(left + width - 2 * rx, top + height - 2 * ry, left+ width, top + height);
angleoval[2].set(left, top + height - 2 * ry, left + 2 * rx, top+ height);
angleoval[3].set(left, top, left + 2 * rx, top + 2 * ry);
}
public void drawroundRectLine(Canvas canvas, int timeIndex, int totalTime) {
pen.setAntiAlias(true); // 消除锯齿
pen.setStyle(Style.STROKE);
int curAngle = 360 - timeIndex * 360 / totalTime;
if (curAngle >= 0 && curAngle <= 60) {
pen.setColor(Color.RED);
int curX = (int) (roundRect.left + rx + curAngle
* (roundRect.right - roundRect.left - 2 * rx) / 60);
int cury = (int) roundRect.top;
canvas.drawLine(roundRect.left + rx, roundRect.top, curX, cury, pen);
}
if (curAngle > 60 && curAngle <= 90) {
pen.setColor(Color.RED);
int curovalangle = 90 * (curAngle - 60) / 30;
canvas.drawArc(angleoval[0], 270, curovalangle, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
if (curAngle > 90 && curAngle <= 150) {
pen.setColor(Color.YELLOW);
int curX = (int) roundRect.right;
int cury = (int) (roundRect.top + ry + (curAngle - 90)
* (roundRect.bottom - roundRect.top - 2 * ry) / 60);
canvas.drawLine(roundRect.right, roundRect.top + ry, curX, cury,
pen);
canvas.drawArc(angleoval[0], 270, 90, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
if (curAngle > 150 && curAngle <= 180) {
pen.setColor(Color.YELLOW);
int curovalangle = 90 * (curAngle - 150) / 30;
canvas.drawArc(angleoval[1], 0, curovalangle, false, pen);
canvas.drawLine(roundRect.right, roundRect.top + ry,
roundRect.right, roundRect.bottom - ry, pen);
canvas.drawArc(angleoval[0], 270, 90, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
if (curAngle > 180 && curAngle <= 240) {
pen.setColor(Color.GREEN);
int curX = (int) (roundRect.right - rx - (curAngle - 180)
* (roundRect.right - roundRect.left - 2 * rx) / 60);
int cury = (int) roundRect.bottom;
canvas.drawLine(roundRect.right - rx, roundRect.bottom, curX, cury,
pen);
canvas.drawArc(angleoval[1], 0, 90, false, pen);
canvas.drawLine(roundRect.right, roundRect.top + ry,
roundRect.right, roundRect.bottom - ry, pen);
canvas.drawArc(angleoval[0], 270, 90, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
if (curAngle > 240 && curAngle <= 270) {
pen.setColor(Color.GREEN);
int curovalangle = 90 * (curAngle - 240) / 30;
canvas.drawArc(angleoval[2], 90, curovalangle, false, pen);
canvas.drawLine(roundRect.right - rx, roundRect.bottom,
roundRect.left + rx, roundRect.bottom, pen);
canvas.drawArc(angleoval[1], 0, 90, false, pen);
canvas.drawLine(roundRect.right, roundRect.top + ry,
roundRect.right, roundRect.bottom - ry, pen);
canvas.drawArc(angleoval[0], 270, 90, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
if (curAngle > 270 && curAngle <= 330) {
pen.setColor(Color.GREEN);
int curX = (int) roundRect.left;
int cury = (int) (roundRect.bottom - ry - (curAngle - 270)
* (roundRect.bottom - roundRect.top - 2 * ry) / 60);
canvas.drawLine(roundRect.left, roundRect.bottom - ry, curX, cury,
pen);
canvas.drawArc(angleoval[2], 90, 90, false, pen);
canvas.drawLine(roundRect.right - rx, roundRect.bottom,
roundRect.left + rx, roundRect.bottom, pen);
canvas.drawArc(angleoval[1], 0, 90, false, pen);
canvas.drawLine(roundRect.right, roundRect.top + ry,
roundRect.right, roundRect.bottom - ry, pen);
canvas.drawArc(angleoval[0], 270, 90, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
if (curAngle > 330 && curAngle <= 360) {
pen.setColor(Color.GREEN);
int curovalangle = 90 * (curAngle - 330) / 30;
canvas.drawArc(angleoval[3], 180, curovalangle, false, pen);
canvas.drawLine(roundRect.left, roundRect.bottom - ry,
roundRect.left, roundRect.top + ry, pen);
canvas.drawArc(angleoval[2], 90, 90, false, pen);
canvas.drawLine(roundRect.right - rx, roundRect.bottom,
roundRect.left + rx, roundRect.bottom, pen);
canvas.drawArc(angleoval[1], 0, 90, false, pen);
canvas.drawLine(roundRect.right, roundRect.top + ry,
roundRect.right, roundRect.bottom - ry, pen);
canvas.drawArc(angleoval[0], 270, 90, false, pen);
canvas.drawLine(roundRect.left + rx, roundRect.top, roundRect.right
- rx, roundRect.top, pen);
}
}
}
调用:
public final static int OPERATETIME = 1000
RoundRectLine rectLine = new RoundRectLine();
rectLine.setRoundRectLine(100,100,200,200,50,50,10);
if (timeIndex <= GAME_OPERATETIME) {
rectLine.drawroundRectLine(canvas, timeIndex, GAME_OPERATETIME);
}timeIndex+= 20;
if(timeIndex > GAME_OPERATETIME){
timeIndex = 0;
}