Package Com.zihao.radar.view;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Random;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.Paint.Style;
Import Android.util.AttributeSet;
Import Android.view.View;
Import COM.ZIHAO.RADAR.R; /** * @ClassName: Radarview * @Description:todo< Radar Scan View > * @author: Zihao * @date: November 11, 2015 Morning 12:26:11 * @v
ersion:v1.1 */@SuppressLint ("Drawallocation") public class Radarview extends View {private context mcontext; Private Boolean issearching = false;//identity is in scan state, default is not scan state private Paint mpaint;//Brush private Bitmap mscanbmp;// Perform scan motion picture private int Moffsetargs = 0;//Scan Movement offset parameter Private Bitmap mdefaultpointbmp;//identifies the device's dot-default private Bitmap m lightpointbmp;//identifies the device's dot-highlight private int Mpointcount= 0;//total number of dots private list<string> Mpointarray = new arraylist<string> ();//Map Private Random for offset
Om = new Random (); private int mwidth, mheight;//wide-high int moutwidth;//outer circle width (w/4/5*2=w/10) int mCx, mcy;//x, y axis center point int Moutsideradius, minsideradius;//Outer and inner circle radius public Radarview (context, AttributeSet attrs, int defstyleattr) {Super
, Attrs, defstyleattr);
TODO auto-generated Constructor stub init (context);
Public Radarview (context, AttributeSet attrs) {Super (context, attrs);
TODO auto-generated Constructor stub init (context);
Public Radarview {Super (context);
TODO auto-generated Constructor stub init (context); /** * todo< Early initialization of objects that need to be used to avoid multiple initialization during the drawing process > * * @return void/private void init (Context cont
EXT) {mpaint = new Paint ();
This.mcontext = context; This.mdefaultpointbmp = Bitmap.createbItmap (bitmapfactory. Decoderesource (Mcontext.getresources (), R.drawable.radar_default_point_ico)); This.mlightpointbmp = Bitmap.createbitmap (Bitmapfactory.decoderesource (mcontext.getresources), R.drawable.
Radar_light_point_ico));
/** * Measures the view and its contents to determine the width and height measured (measurement gets control dimensions). */@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated met
Hod stub super.onmeasure (Widthmeasurespec, Heightmeasurespec);
Gets the control area width High if (mwidth = 0 | | mheight = = 0) {final int minimumwidth = Getsuggestedminimumwidth ();
Final int minimumheight = Getsuggestedminimumheight ();
Mwidth = resolvemeasured (Widthmeasurespec, minimumwidth);
Mheight = resolvemeasured (Heightmeasurespec, minimumheight); Mscanbmp = Bitmap.createscaledbitmap (Bitmapfactory.decoderesource (Mcontext.getresources (), R.DRAWABLE.RADAR_SC AN_IMG), Mwidth-moutwidth, MWIdth-moutwidth, false);
Get x/y Axis center point mCx = MWIDTH/2;
Mcy = MHEIGHT/2;
Gets the outer circle width moutwidth = MWIDTH/10; Calculate the inner and outer radius Moutsideradius = mwidth/2;//radius of the outer circle Minsideradius = (mwidth-moutwidth)/4/2;//The radius of the inner circle, in addition to the outermost, its Its circle radius = Layer *insideradius}}/** * Draw View-draw from the external to the interior/@Override protected void OnDraw (Canvas Canvas
) {//TODO auto-generated Method stub super.ondraw (canvas); Start drawing the outermost circle Mpaint.setantialias (true);//Set anti-aliasing Mpaint.setstyle (Style.fill);//Set Fill style Mpaint.setcolor (0xffB
8DCFC)//Set Brush color//1. Start drawing round canvas.drawcircle (mCx, Mcy, Moutsideradius, Mpaint);
Start drawing Inner 4 round mpaint.setcolor (0XFF3278B4);
Canvas.drawcircle (MCx, Mcy, Minsideradius * 4, Mpaint);
Start drawing Inner 3 round Mpaint.setstyle (Style.stroke);
Mpaint.setcolor (0XFF31C9F2);
Canvas.drawcircle (MCx, Mcy, Minsideradius * 3, Mpaint); Start drawing inside 2 round canvas.drawcircle (mCx, Mcy, Minsideradius * 2, Mpaint);
Start drawing within 1 round canvas.drawcircle (mCx, Mcy, Minsideradius * 1, mpaint); 2. Start Drawing diagonal Canvas.drawline (MOUTWIDTH/2, Mcy, MWIDTH-MOUTWIDTH/2, Mcy, mpaint);//Draw 0°~180° Diagonal Canvas.dra Wline (MCx, MHEIGHT-MOUTWIDTH/2, MCx, MOUTWIDTH/2, mpaint);//Draw 90°~270° diagonal//Diagonal int STA based on angle
RtX, Starty, EndX, EndY;
Double Radian; Draw 45°~225° Diagonal//Calculate start position x/y coordinate point radian = Math.toradians ((double) 45)//convert angle to radians startx = (int) (mCx + mIn Sideradius * 4 * MATH.COS (Radian); Calculates a point of the current circumference by the center coordinate, radius, and current angle Starty = (int) (Mcy + Minsideradius * 4 * Math.sin (R)
Adian))//calculates the end position x/y coordinates radian = Math.toradians ((double) 45 + 180) by calculating a point ordinate/in the current circle at the center coordinate, radius, and current angle;
EndX = (int) (MCx + Minsideradius * 4 * MATH.COS (radian));
EndY = (int) (Mcy + Minsideradius * 4 * Math.sin (radian));
Canvas.drawline (StartX, Starty, EndX, EndY, Mpaint); Draw 135°~315° Diagonal//Calculate start position x/y coordinate point
Radian = Math.toradians ((double) 135);
STARTX = (int) (MCx + Minsideradius * 4 * MATH.COS (radian));
Starty = (int) (Mcy + Minsideradius * 4 * Math.sin (radian));
Computes the end position x/y coordinate point radian = Math.toradians (double) 135 + 180);
EndX = (int) (MCx + Minsideradius * 4 * MATH.COS (radian));
EndY = (int) (Mcy + Minsideradius * 4 * Math.sin (radian));
Canvas.drawline (StartX, Starty, EndX, EndY, Mpaint);
3. Draw the scan sector Diagram Canvas.save ()//To save the state of the canvas. Save, you can call the canvas translation, scaling, rotation, error-cut, cropping and other operations.
if (issearching) {//judge whether to be in scan Canvas.rotate (Moffsetargs, mCx, mcy);//Draw rotation angle, parameter one: angle; parameter two: x Center; parameter three: Y center. Canvas.drawbitmap (Mscanbmp, Mcx-mscanbmp.getwidth ()/2, Mcy-mscanbmp.getheight ()/2, NULL);//Draw bitmap Scan map
Film effect Moffsetargs + 3; else {canvas.drawbitmap (mscanbmp, Mcx-mscanbmp.getwidth ()/2, Mcy-mscanbmp.getheight ()/2, NULL
); //4. Start drawing dynamic point Canvas.restore ()//The state that was saved before the canvas was restored. Anti-The actions performed on canvas after save have an effect on subsequent rendering. if (Mpointcount > 0) {//When the total number of dots >0, go to the next level to determine if (Mpointcount > Mpointarray.size ()) {//When the total number of dots is greater than the number of storage coordinates, the description is increased
Plus, you need to regenerate the random coordinate point int mx = Minsideradius + mrandom.nextint (Minsideradius * 6);
int my = Minsideradius + mrandom.nextint (Minsideradius * 6);
Mpointarray.add (mx + "/" + my); //start drawing coordinate for (int i = 0; i < mpointarray.size (); i++) {string[] result = Mpointarray.get (
i). Split ("/"); Start drawing dynamic Point if (I < mpointarray.size ()-1) canvas.drawbitmap (mdefaultpointbmp, Integ
Er.parseint (Result[0]), Integer.parseint (result[1]), NULL); else Canvas.drawbitmap (mlightpointbmp, Integer.parseint (result[0)), Integer.parse
Int (result[1]), NULL);
} if (issearching) this.invalidate (); /** * todo< Set Scan status > * * @return void */PUblic void Setsearching (Boolean status) {this.issearching = status;
This.invalidate ();
/** * todo< New dynamic Point > * * @return void */public void Addpoint () {mpointcount++;
This.invalidate (); /** * todo< resolution FETCH CONTROL WIDTH > * * @return int */private int resolvemeasured (int measurespec, int d
esired) {int result = 0;
int specsize = measurespec.getsize (Measurespec);
Switch (Measurespec.getmode (measurespec)) {case MeasureSpec.UNSPECIFIED:result = desired;
Break
Case MeasureSpec.AT_MOST:result = math.min (specsize, desired);
Break
Case MeasureSpec.EXACTLY:default:result = specsize;
return result; }
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.