안 드 로 이 드 레이더 스캐닝 동적 인터페이스 제작
원본 코드:
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>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.