編輯:Android開發實例
本文實例講述了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應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
在Android 5.0之後引入了MD風格,從而狀態欄沉浸也成為了一種設計習慣。而停留在之Andr
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
現在的Android應用,只要有一個什麼新的創意,過不了多久,幾乎所有的應用都帶這個創意。這不,咱們公司最近的一個持續性的項目,想在首頁加個從左滑動出來的菜單,我