看图心动没??
下面我们来看看代码吧,干货时间到;
1.项目结构
2.第一步新建一个calss,就起名叫Mi吧。一会用来绘制自定义View。
第二部在xml文件中放置插件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity">
<com.example.sersortest.Mi
android:id="@+id/miui"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步写调用方向传感器的代码
package com.example.sersortest;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private SensorEventListener sensorEventListener;
private Mi miui;
private float val;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
miui =findViewById(R.id.miui);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
val = event.values[0];
miui.setVal(val);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onDestroy() {
super.onDestroy();
sensorManager.unregisterListener(sensorEventListener);
}
}
最后颜色样式colors.xml的文件给你们。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000</color>
<color name="darkRed">#702216</color>
<color name="lightGray">#323232</color>
<color name="deepGray">#8B8B8B</color>
<color name="white">#fff</color>
<color name="red">#f00</color>
<color name="ocbg">#237EAD</color>
</resources>
最后核心自定义View ,Mi.java的代码
package com.example.sersortest;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class Mi extends View {
private Canvas mCanvas;
private Context mContext;
//View矩形的宽度
private int width;
//指南针圆心点坐标
private int mCenterX;
private int mCenterY;
//外圆半径
private int mOutSideRadius;
//外接圆半径
private int mCircumRadius;
//指南针文字大小空间高度
private int mTextHeight;
//暗红色 外圈笔
private Paint mDarkRedPaint;
//深灰 外圈笔
private Paint mDeepGrayPaint;
//外三角笔
private Paint mOutSideCircumPaint;
//浅灰 外圈笔
private Paint mLightGrayPaint;
//指南针上面 文字笔
private Paint mTextPaint;
//外接圆,三角形笔
private Paint mCircumPaint;
//指南针上面文字的外接矩形,用来测文字大小让文字居中
private Rect mTextRect;
//外圈小三角形的Path
private Path mOutsideTriangle;
//外接圆小三角形的Path
private Path mCircumTriangle;
//NESW 文字笔 和文字外接矩形
private Paint mNorthP