服务器之家

服务器之家 > 正文

Android编程简单实现雷达扫描效果

时间:2021-04-07 15:25     来源/作者:freesonhp

本文实例讲述了Android编程简单实现雷达扫描效果。分享给大家供大家参考,具体如下:

在eoe看到有一篇关于雷达扫描的文章,然后看了下,很简单,但是觉得还有很多可以优化的地方,下面贴出来

?
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
package com.example.wave;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.widget.FrameLayout;
public class RadarView extends FrameLayout {
 private int viewSize = 500;
 private Paint mPaintLine;
 private Paint mPaintSector;
 private boolean isStart = false;
 private ScanThread mThread;
 private int start = 0;
 private final int paintWidth = 10;
 public RadarView(Context context) {
  super(context);
  init();
 }
 public RadarView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }
 private void init() {
  initPaint();
  mThread = new ScanThread();
  setBackgroundColor(Color.TRANSPARENT);
 }
 private void initPaint() {
  mPaintLine = new Paint();
  mPaintLine.setStrokeWidth(paintWidth);
  mPaintLine.setAntiAlias(true);
  mPaintLine.setStyle(Style.STROKE);
  mPaintLine.setColor(0xffff0000);
  mPaintSector = new Paint();
  mPaintSector.setColor(0x9D00ff00);
  mPaintSector.setAntiAlias(true);
 }
 public void setViewSize(int size) {
  this.viewSize = size;
  mShader = new SweepGradient(viewSize >> 1, viewSize >> 1, Color.TRANSPARENT, Color.GREEN);
  setMeasuredDimension(viewSize, viewSize);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  setMeasuredDimension(viewSize, viewSize);
 }
 public void start() {
  mThread.start();
  isStart = true;
 }
 public void stop() {
  if (isStart) {
   Thread.interrupted();
   isStart = false;
  }
 }
 private Shader mShader = new SweepGradient(viewSize >> 1, viewSize >> 1, Color.TRANSPARENT, Color.GREEN);
 private Matrix matrix = new Matrix();
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  int halfRadaSize = viewSize >> 1;
  canvas.drawCircle(halfRadaSize, halfRadaSize, halfRadaSize >> 1, mPaintLine);
  canvas.drawCircle(halfRadaSize, halfRadaSize, halfRadaSize - (paintWidth >> 1), mPaintLine);
  canvas.drawLine(halfRadaSize, 0, halfRadaSize, viewSize, mPaintLine);
  canvas.drawLine(0, halfRadaSize, viewSize, halfRadaSize, mPaintLine);
  mPaintSector.setShader(mShader);
  canvas.concat(matrix);
  canvas.drawCircle(halfRadaSize, halfRadaSize, halfRadaSize - paintWidth, mPaintSector);
 }
 protected class ScanThread extends Thread {
  int halfRadaSize = viewSize >> 1;
  @Override
  public void run() {
   while (isStart) {
    start = start + 2;
    matrix.reset();
    matrix.postRotate(start, halfRadaSize, halfRadaSize);
    postInvalidate();
    try {
     Thread.sleep(10);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

希望本文所述对大家Android程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部