Android编程简单实现雷达扫描效果
作者:freesonhp 时间:2021-06-17 20:38:04
本文实例讲述了Android编程简单实现雷达扫描效果。分享给大家供大家参考,具体如下:
在eoe看到有一篇关于雷达扫描的文章,然后看了下,很简单,但是觉得还有很多可以优化的地方,下面贴出来
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程序设计有所帮助。
标签:Android,扫描
0
投稿
猜你喜欢
Java环境下高德地图Api的使用方式
2022-06-13 06:43:59
Spring Boot @Conditional注解用法示例介绍
2023-04-18 22:51:51
一篇文章让你弄懂Java运算符
2023-12-02 20:16:41
java启动线程的3种方式对比分析
2023-12-17 08:38:41
Spring boot 连接多数据源过程详解
2023-11-28 12:09:51
C#保存listbox中数据到文本文件的方法
2023-02-20 10:37:27
springboot下ueditor上传功能的实现及遇到的问题
2023-09-22 05:46:21
java解决动态配置字段需求问题
2022-08-03 17:38:30
一文带你探究Spring中Bean的线程安全性问题
2023-10-03 10:52:13
C#实现计算年龄的简单方法汇总
2022-12-26 01:12:41
java.net.SocketException: Connection reset 解决方法
2023-06-17 01:28:43
C# TrackBar拖动条改变滑块颜色
2021-12-22 22:11:07
java 垃圾回收机制以及经典垃圾回收器详解
2022-07-06 05:16:08
c语言实现的几种常用排序算法
2022-02-23 04:04:11
Unity实现文本转贴图
2022-05-10 19:53:04
Android应用中使用SharedPreferences类存储数据的方法
2022-06-11 20:27:08
详解Java泛型及其应用
2023-09-21 22:38:32
C# 复制与删除文件的实现方法
2022-01-23 02:36:06
springboot如何引入外部yml配置文件
2023-11-07 07:35:38
C#编程实现统计文件夹内文件和隐藏文件的方法示例
2022-09-29 16:09:38