Android自定義實(shí)現(xiàn)羅盤(pán)視圖詳解
在開(kāi)發(fā)Android應(yīng)用時(shí),自定義視圖是一個(gè)非常重要的技能。本文將介紹如何創(chuàng)建一個(gè)自定義的羅盤(pán)視圖(CompassView),該視圖可以顯示設(shè)備的方向。我們將通過(guò)使用??SensorManager??來(lái)獲取方向數(shù)據(jù),并使用自定義繪圖方法來(lái)繪制羅盤(pán)。
1. 創(chuàng)建項(xiàng)目
首先,在Android Studio中創(chuàng)建一個(gè)新的項(xiàng)目,選擇“Empty Activity”模板,命名為??CustomCompass??。
2. 添加權(quán)限
為了能夠訪(fǎng)問(wèn)傳感器數(shù)據(jù),需要在??AndroidManifest.xml??文件中添加相應(yīng)的權(quán)限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
3. 創(chuàng)建自定義視圖
3.1 創(chuàng)建CompassView類(lèi)
在??app/src/main/java/com/example/customcompass/??目錄下創(chuàng)建一個(gè)新的Java類(lèi)??CompassView.java??:
package com.example.customcompass;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.view.View;
public class CompassView extends View implements SensorEventListener {
private Paint paint;
private float direction = 0f;
private SensorManager sensorManager;
private Sensor accelerometer;
private Sensor magnetometer;
public CompassView(Context context) {
super(context);
init(context);
}
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CompassView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(50);
paint.setAntiAlias(true);
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
int centerX = width / 2;
int centerY = height / 2;
// 繪制圓形背景
paint.setColor(Color.LTGRAY);
canvas.drawCircle(centerX, centerY, Math.min(width, height) / 2, paint);
// 繪制指針
paint.setColor(Color.RED);
canvas.drawLine(centerX, centerY, (float) (centerX + Math.cos(Math.toRadians(direction)) * 150),
(float) (centerY - Math.sin(Math.toRadians(direction)) * 150), paint);
// 繪制方向文本
paint.setColor(Color.BLACK);
canvas.drawText("N", centerX, centerY - 100, paint);
canvas.drawText("S", centerX, centerY + 100, paint);
canvas.drawText("E", centerX + 100, centerY, paint);
canvas.drawText("W", centerX - 100, centerY, paint);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, mGravity, 0, 3);
} else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, mGeomagnetic, 0, 3);
}
boolean success = SensorManager.getRotationMatrix(mRotationMatrix, null, mGravity, mGeomagnetic);
if (success) {
SensorManager.getOrientation(mRotationMatrix, mOrientationAngles);
direction = (float) Math.toDegrees(mOrientationAngles[0]);
invalidate(); // 重繪視圖
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
private float[] mGravity = new float[3];
private float[] mGeomagnetic = new float[3];
private float[] mRotationMatrix = new float[9];
private float[] mOrientationAngles = new float[3];
public void registerSensor() {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}
public void unregisterSensor() {
sensorManager.unregisterListener(this);
}
}3.2 在布局文件中使用CompassView
在??activity_main.xml??中添加??CompassView??:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<com.example.customcompass.CompassView
android:id="@+id/compassView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true" />
</RelativeLayout>
4. 在MainActivity中注冊(cè)和注銷(xiāo)傳感器
在??MainActivity.java??中注冊(cè)和注銷(xiāo)傳感器:
package com.example.customcompass;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private CompassView compassView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
compassView = findViewById(R.id.compassView);
compassView.registerSensor();
}
@Override
protected void onDestroy() {
super.onDestroy();
compassView.unregisterSensor();
}
}5. 運(yùn)行應(yīng)用
現(xiàn)在,你可以運(yùn)行你的應(yīng)用了。你應(yīng)該會(huì)看到一個(gè)羅盤(pán)視圖,它會(huì)隨著設(shè)備的方向變化而更新。
通過(guò)上述步驟,我們成功地創(chuàng)建了一個(gè)自定義的羅盤(pán)視圖。這個(gè)視圖利用了Android的傳感器管理器來(lái)獲取方向數(shù)據(jù),并使用自定義繪圖方法來(lái)顯示羅盤(pán)。
6.方法補(bǔ)充
在Android應(yīng)用開(kāi)發(fā)中,自定義羅盤(pán)視圖是一個(gè)常見(jiàn)的需求,特別是在導(dǎo)航、戶(hù)外活動(dòng)等應(yīng)用中。下面我將提供一個(gè)簡(jiǎn)單的示例,展示如何創(chuàng)建一個(gè)自定義的羅盤(pán)視圖。這個(gè)例子將包括基本的布局文件、自定義視圖類(lèi)以及使用傳感器數(shù)據(jù)來(lái)更新羅盤(pán)方向。
方法一
添加權(quán)限
首先,在??AndroidManifest.xml??文件中添加必要的權(quán)限,以允許應(yīng)用程序訪(fǎng)問(wèn)設(shè)備的方向傳感器:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-feature android:name="android.hardware.sensor.compass" />
創(chuàng)建布局文件
創(chuàng)建一個(gè)新的布局文件??activity_main.xml??,用于顯示羅盤(pán)視圖:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<com.example.myapp.CompassView
android:id="@+id/compassView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true" />
</RelativeLayout>
創(chuàng)建自定義羅盤(pán)視圖
接下來(lái),創(chuàng)建一個(gè)自定義的羅盤(pán)視圖??CompassView.java??。這個(gè)視圖將繪制一個(gè)圓形的羅盤(pán),并根據(jù)傳入的角度旋轉(zhuǎn)指針:
package com.example.myapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class CompassView extends View {
private Paint paint;
private float direction = 0f; // 方向角度
public CompassView(Context context) {
super(context);
init();
}
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CompassView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5f);
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
int radius = Math.min(width, height) / 2 - 10;
// 繪制圓
canvas.drawCircle(width / 2, height / 2, radius, paint);
// 繪制指針
paint.setColor(Color.RED);
canvas.save();
canvas.rotate(-direction, width / 2, height / 2); // 逆時(shí)針旋轉(zhuǎn)
canvas.drawLine(width / 2, height / 2 - radius, width / 2, height / 2 - 50, paint);
canvas.restore();
// 繪制N、S、E、W
paint.setTextSize(40);
paint.setColor(Color.BLACK);
canvas.drawText("N", width / 2 - 10, height / 2 - radius + 40, paint);
canvas.drawText("S", width / 2 - 10, height / 2 + radius - 10, paint);
canvas.drawText("E", width / 2 + radius - 40, height / 2 + 10, paint);
canvas.drawText("W", width / 2 - radius + 10, height / 2 + 10, paint);
}
public void setDirection(float direction) {
this.direction = direction;
invalidate(); // 重繪視圖
}
}使用傳感器數(shù)據(jù)更新羅盤(pán)方向
在主活動(dòng)中,注冊(cè)一個(gè)傳感器監(jiān)聽(tīng)器來(lái)獲取設(shè)備的方向變化,并更新羅盤(pán)視圖的方向:
package com.example.myapp;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor rotationSensor;
private CompassView compassView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
compassView = findViewById(R.id.compassView);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, rotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
float[] orientation = new float[3];
SensorManager.getOrientation(rotationMatrix, orientation);
// 將弧度轉(zhuǎn)換為角度
float azimuth = (float) Math.toDegrees(orientation[0]);
compassView.setDirection(azimuth);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// 不處理精度變化
}
}運(yùn)行應(yīng)用
現(xiàn)在你可以運(yùn)行這個(gè)應(yīng)用,它會(huì)顯示一個(gè)自定義的羅盤(pán)視圖,并且隨著設(shè)備的旋轉(zhuǎn)而更新方向。
方法二
這個(gè)示例展示了如何在Android中創(chuàng)建一個(gè)簡(jiǎn)單的自定義羅盤(pán)視圖。你可以根據(jù)需要進(jìn)一步擴(kuò)展和美化這個(gè)視圖,例如添加更多的圖形元素或改進(jìn)用戶(hù)界面。在Android中創(chuàng)建一個(gè)自定義的羅盤(pán)視圖涉及多個(gè)步驟,包括定義視圖、處理傳感器數(shù)據(jù)、繪制羅盤(pán)圖像等。下面是一個(gè)詳細(xì)的指南,幫助你實(shí)現(xiàn)一個(gè)基本的自定義羅盤(pán)視圖。
創(chuàng)建自定義視圖
首先,你需要?jiǎng)?chuàng)建一個(gè)自定義視圖類(lèi)來(lái)繪制羅盤(pán)。這個(gè)類(lèi)將繼承自 ??View?? 類(lèi),并重寫(xiě) ??onDraw?? 方法來(lái)繪制羅盤(pán)圖像。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
public class CompassView extends View {
private Bitmap compassBitmap;
private Matrix matrix;
private float currentDegree = 0f;
public CompassView(Context context) {
super(context);
init();
}
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CompassView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
compassBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass);
matrix = new Matrix();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
matrix.reset();
matrix.postRotate(-currentDegree, compassBitmap.getWidth() / 2, compassBitmap.getHeight() / 2);
canvas.drawBitmap(compassBitmap, matrix, null);
}
public void updateDegree(float degree) {
currentDegree = degree;
invalidate(); // 重新繪制視圖
}
}處理傳感器數(shù)據(jù)
為了獲取設(shè)備的方向數(shù)據(jù),你需要注冊(cè)一個(gè) ??SensorEventListener?? 并監(jiān)聽(tīng) ??TYPE_ORIENTATION?? 傳感器(或更現(xiàn)代的 ??TYPE_ROTATION_VECTOR?? 傳感器)。
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor sensor;
private CompassView compassView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
compassView = findViewById(R.id.compassView);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float degree = Math.round(event.values[0]);
compassView.updateDegree(degree);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// 無(wú)需處理
}
}布局文件
在布局文件中添加自定義的 ??CompassView??。
<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"
tools:context=".MainActivity">
<com.example.yourapp.CompassView
android:id="@+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
添加資源文件
確保你有一個(gè)羅盤(pán)圖像資源文件(例如 ??res/drawable/compass.png??),并將其放置在項(xiàng)目的 ??res/drawable?? 目錄下。
權(quán)限
在 ??AndroidManifest.xml?? 中添加必要的權(quán)限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
總結(jié)
以上步驟展示了如何在Android中創(chuàng)建一個(gè)自定義的羅盤(pán)視圖。通過(guò)自定義視圖類(lèi)和傳感器事件監(jiān)聽(tīng)器,你可以實(shí)現(xiàn)一個(gè)動(dòng)態(tài)更新方向的羅盤(pán)。
以上就是Android自定義實(shí)現(xiàn)羅盤(pán)視圖詳解的詳細(xì)內(nèi)容,更多關(guān)于Android自定義視圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開(kāi)發(fā)手冊(cè)TextInputLayout樣式使用示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)TextInputLayout樣式使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android實(shí)現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
android自定義Camera實(shí)現(xiàn)錄像和拍照
這篇文章主要為大家詳細(xì)介紹了android自定義Camera實(shí)現(xiàn)錄像和拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android6.0開(kāi)發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
這篇文章主要介紹了Android6.0開(kāi)發(fā)中屏幕旋轉(zhuǎn)原理與流程,結(jié)合實(shí)例形式詳細(xì)分析了Android6.0屏幕旋轉(zhuǎn)的原理與相關(guān)實(shí)現(xiàn)流程,并附帶了Android動(dòng)態(tài)開(kāi)啟與禁用屏幕旋轉(zhuǎn)的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-11-11
Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果
這篇文章主要介紹了Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果的方法,借助Fragment可以使Activity的內(nèi)部管理邏輯更加清晰,需要的朋友可以參考下2016-03-03
Android開(kāi)發(fā)RecyclerView單獨(dú)刷新使用技巧
本篇文章主要是分享下RecyclerView中子item如何單獨(dú)刷新以及子item的某一部分內(nèi)容如何實(shí)現(xiàn)單獨(dú)刷新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自定義倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Kotlin中?和!!的區(qū)別詳細(xì)對(duì)比
這篇文章主要給大家介紹了關(guān)于Kotlin中?和!!區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
這篇文章主要介紹了Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能的相關(guān)資料,需要的朋友可以參考下2016-02-02

