廢話不多說鲜滩,先看效果。
如何現(xiàn)實(shí)
代碼里面也注釋得很清楚。掃描效果就是通過SweepGradient來實(shí)現(xiàn)的加上變換Matrix來現(xiàn)實(shí)的。
實(shí)現(xiàn)步驟:
1.先實(shí)現(xiàn)圓環(huán)
2.SweepGradient現(xiàn)實(shí)掃描渲染
3.用Matrix旋轉(zhuǎn)變換讓SweepGradient旋轉(zhuǎn)掃描起來
1.先實(shí)現(xiàn)圓環(huán)律杠。很簡單,應(yīng)該不用詳說竞惋。下面就貼代碼柜去。
//圓圈大小的比例
private float[] circleProportion = {1 / 13f, 2 / 13f, 3 / 13f, 4 / 13f, 5 / 13f, 6 / 13f};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//圓環(huán)
drawCircle(canvas);
}
private void drawCircle(Canvas canvas) {
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[0], mPaintCircle);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[1], mPaintCircle);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[2], mPaintCircle);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[3], mPaintCircle);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[4], mPaintCircle);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[5], mPaintCircle);
}
效果是:
2.SweepGradient現(xiàn)實(shí)掃描渲染
不知道SweepGradient可以看一下這篇文章(后面有時(shí)間我也寫一篇,絕對不是我懶拆宛。好吧嗓奢。我承認(rèn)我懶)
http://blog.csdn.net/t12x3456/article/details/10473225
//初始化掃描渲染
mSweepGradient = new SweepGradient(mWidth / 2, mHeight / 2, new int[]{Color.TRANSPARENT,
Color.parseColor("#84B5CA")}, null);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//畫一環(huán)環(huán)的圓圈
drawCircle(canvas);
//畫掃描
drawScan(canvas);
}
private void drawScan(Canvas canvas) {
//給畫筆設(shè)置渲染效果
mPaintScan.setShader(mSweepGradient);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[4], mPaintScan);
}
效果:
3.用Matrix旋轉(zhuǎn)變換讓SweepGradient旋轉(zhuǎn)掃描起來
不懂Matrix可以看這篇博客:
http://www.gcssloop.com/customview/Matrix_Basic
Matrix matrix = new Matrix();
int speed = 5;
private Runnable run = new Runnable() {
@Override
public void run() {
matrix.preRotate(speed,mWidth/2,mHeight/2);
invalidate();
postDelayed(run,130);
}
};
private void drawScan(Canvas canvas) {
//給畫筆設(shè)置渲染效果
mPaintScan.setShader(mSweepGradient);
canvas.concat(matrix);
canvas.drawCircle(mWidth / 2, mHeight / 2, maxRadius * circleProportion[4], mPaintScan);
}
具體代碼見:
https://github.com/zsj1225/RadarView
參考:
http://blog.csdn.net/t12x3456/article/details/10473225
http://www.gcssloop.com/customview/Matrix_Basic
http://blog.csdn.net/mr_immortalz/article/details/51319354