欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-23 21:21:16
本文实例讲述了android编程简单实现雷达扫描效果。分享给大家供大家参考,具体如下: 在eoe看到有一篇关于雷达扫描的文章,然后看了下,很简单,但是觉得还有很多可以优化...

本文实例讲述了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程序设计有所帮助。