안 드 로 이 드 레이더 스캐닝 동적 인터페이스 제작

먼저 효과 도 보기:

원본 코드:

package com.zihao.radar; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 
 
import com.zihao.radar.view.RadarView; 
 
public class MainActivity extends Activity { 
 
  private RadarView mRadarView; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    //       
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 
    mRadarView = (RadarView) findViewById(R.id.radar_view); 
    mRadarView.setSearching(true); 
    mRadarView.addPoint(); 
    mRadarView.addPoint(); 
  } 
 
} 

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<      > 
 * @author:zihao 
 * @date:2015 11 11    12:26:11 
 * @version:v1.1 
 */ 
@SuppressLint("DrawAllocation") 
public class RadarView extends View { 
 
  private Context mContext; 
  private boolean isSearching = false;//           ,          
  private Paint mPaint;//    
  private Bitmap mScanBmp;//           
  private int mOffsetArgs = 0;//           
  private Bitmap mDefaultPointBmp;//        -   
  private Bitmap mLightPointBmp;//        -   
  private int mPointCount = 0;//      
  private List<String> mPointArray = new ArrayList<String>();//       map 
  private Random mRandom = new Random(); 
  private int mWidth, mHeight;//    
  int mOutWidth;//     (w/4/5*2=w/10) 
  int mCx, mCy;// x、y     
  int mOutsideRadius, mInsideRadius;//  、     
 
  public RadarView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    // TODO Auto-generated constructor stub 
    init(context); 
  } 
 
  public RadarView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
    init(context); 
  } 
 
  public RadarView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    init(context); 
  } 
 
  /** 
   * TODO<             ,             > 
   * 
   * @return void 
   */ 
  private void init(Context context) { 
    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)); 
  } 
 
  /** 
   *         ,            (        ). 
   */ 
  @Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // TODO Auto-generated method stub 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 
    //          
    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_scan_img), mWidth 
          - mOutWidth, mWidth - mOutWidth, false); 
 
      //   x/y     
      mCx = mWidth / 2; 
      mCy = mHeight / 2; 
 
      //        
      mOutWidth = mWidth / 10; 
 
      //    、    
      mOutsideRadius = mWidth / 2;//       
      mInsideRadius = (mWidth - mOutWidth) / 4 / 2;//      ,    ,      =  *insideRadius 
    } 
  } 
 
  /** 
   *     --         
   */ 
  @Override 
  protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 
    super.onDraw(canvas); 
    //           
    mPaint.setAntiAlias(true);//       
    mPaint.setStyle(Style.FILL);//        
    mPaint.setColor(0xffB8DCFC);//        
    // 1.       
    canvas.drawCircle(mCx, mCy, mOutsideRadius, mPaint); 
 
    //      4  
    mPaint.setColor(0xff3278B4); 
    canvas.drawCircle(mCx, mCy, mInsideRadius * 4, mPaint); 
 
    //      3  
    mPaint.setStyle(Style.STROKE); 
    mPaint.setColor(0xff31C9F2); 
    canvas.drawCircle(mCx, mCy, mInsideRadius * 3, mPaint); 
 
    //      2  
    canvas.drawCircle(mCx, mCy, mInsideRadius * 2, mPaint); 
 
    //      1  
    canvas.drawCircle(mCx, mCy, mInsideRadius * 1, mPaint); 
 
    // 2.        
    canvas.drawLine(mOutWidth / 2, mCy, mWidth - mOutWidth / 2, mCy, mPaint);//   0°~180°    
    canvas.drawLine(mCx, mHeight - mOutWidth / 2, mCx, mOutWidth / 2, 
        mPaint);//   90°~270°    
 
    //           
    int startX, startY, endX, endY; 
    double radian; 
 
    //   45°~225°    
    //       x/y    
    radian = Math.toRadians((double) 45);//          
    startX = (int) (mCx + mInsideRadius * 4 * Math.cos(radian));//       、                    
    startY = (int) (mCy + mInsideRadius * 4 * Math.sin(radian));//       、                    
    //       x/y    
    radian = Math.toRadians((double) 45 + 180); 
    endX = (int) (mCx + mInsideRadius * 4 * Math.cos(radian)); 
    endY = (int) (mCy + mInsideRadius * 4 * Math.sin(radian)); 
    canvas.drawLine(startX, startY, endX, endY, mPaint); 
 
    //   135°~315°    
    //       x/y    
    radian = Math.toRadians((double) 135); 
    startX = (int) (mCx + mInsideRadius * 4 * Math.cos(radian)); 
    startY = (int) (mCy + mInsideRadius * 4 * Math.sin(radian)); 
    //       x/y    
    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.        
    canvas.save();//     Canvas   .save  ,    Canvas   、  、  、  、     . 
 
    if (isSearching) {//          
      canvas.rotate(mOffsetArgs, mCx, mCy);//       ,   :  ;   :x  ;   :y  . 
      canvas.drawBitmap(mScanBmp, mCx - mScanBmp.getWidth() / 2, mCy 
          - mScanBmp.getHeight() / 2, null);//   Bitmap       
      mOffsetArgs += 3; 
    } else { 
      canvas.drawBitmap(mScanBmp, mCx - mScanBmp.getWidth() / 2, mCy 
          - mScanBmp.getHeight() / 2, null); 
    } 
 
    // 4.        
    canvas.restore();//     Canvas       .  save  Canvas              . 
 
    if (mPointCount > 0) {//      >0 ,        
 
      if (mPointCount > mPointArray.size()) {//                ,     ,            
        int mx = mInsideRadius + mRandom.nextInt(mInsideRadius * 6); 
        int my = mInsideRadius + mRandom.nextInt(mInsideRadius * 6); 
        mPointArray.add(mx + "/" + my); 
      } 
 
      //         
      for (int i = 0; i < mPointArray.size(); i++) { 
        String[] result = mPointArray.get(i).split("/"); 
 
        //         
        if (i < mPointArray.size() - 1) 
          canvas.drawBitmap(mDefaultPointBmp, 
              Integer.parseInt(result[0]), 
              Integer.parseInt(result[1]), null); 
        else 
          canvas.drawBitmap(mLightPointBmp, 
              Integer.parseInt(result[0]), 
              Integer.parseInt(result[1]), null); 
      } 
    } 
 
    if (isSearching) 
      this.invalidate(); 
  } 
 
  /** 
   * TODO<      > 
   * 
   * @return void 
   */ 
  public void setSearching(boolean status) { 
    this.isSearching = status; 
    this.invalidate(); 
  } 
 
  /** 
   * TODO<     > 
   * 
   * @return void 
   */ 
  public void addPoint() { 
    mPointCount++; 
    this.invalidate(); 
  } 
 
  /** 
   * TODO<        > 
   * 
   * @return int 
   */ 
  private int resolveMeasured(int measureSpec, int desired) { 
    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; 
  } 
} 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="@drawable/bgp" 
  tools:context="com.zihao.radar.MainActivity" > 
 
  <com.zihao.radar.view.RadarView 
    android:id="@+id/radar_view" 
    android:layout_width="600px" 
    android:layout_height="600px" 
    android:layout_centerInParent="true" /> 
 
</RelativeLayout> 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기