android绘制动态圆角矩形时间条

直接上代码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; 
  }
 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值