Q
如何編程指南針?
0
A
回答
0
自定義指南針視圖類:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyCompassView extends View {
private Paint paint;
private float position = 0;
public MyCompassView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
int xPoint = getMeasuredWidth()/2;
int yPoint = getMeasuredHeight()/2;
float radius = (float) (Math.max(xPoint, yPoint) * 0.6);
canvas.drawCircle(xPoint, yPoint, radius, paint);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
// 3.143 is a good approximation for the circle
canvas.drawLine(xPoint,
yPoint,
(float) (xPoint + radius
* Math.sin((double) (-position)/180 * 3.143)),
(float) (yPoint - radius
* Math.cos((double) (-position)/180 * 3.143)), paint);
canvas.drawText(String.valueOf(position), xPoint, yPoint, paint);
}
public void updateData(float position) {
this.position = position;
invalidate();
}
}
主要業務類:
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity {
private static SensorManager sensorService;
private MyCompassView compassView;
private Sensor sensor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compassView = new MyCompassView(this);
setContentView(compassView);
sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorService.getDefaultSensor(Sensor.TYPE_ORIENTATION);
if (sensor != null) {
sensorService.registerListener(mySensorEventListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
Log.i("Compass MainActivity", "Registerered for ORIENTATION Sensor");
} else {
Log.e("Compass MainActivity", "Registerered for ORIENTATION Sensor");
Toast.makeText(this, "ORIENTATION Sensor not found",
Toast.LENGTH_LONG).show();
finish();
}
}
private SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
// angle between the magnetic north directio
// 0=North, 90=East, 180=South, 270=West
float azimuth = event.values[0];
compassView.updateData(azimuth);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (sensor != null) {
sensorService.unregisterListener(mySensorEventListener);
}
}
}
0
您可能已經讀了,但如果沒有,我會建議你去在Android Documentation關於可用的傳感器和您的具體情況 - 這可以幫助您設計指南針應用程序。在網頁中搜索「指南針」將會帶您瞭解更加難懂的相關信息。祝你好運!
+0
hi @ go2 yeaaaa這是真的,試圖找到一些在互聯網上的指南針信息真的很難,搜索結果只顯示安裝Android Compaas應用程序或如何使用它們,而不是如何做到這一點 – Elsanty
相關問題
- 1. 指南針GPS指南針
- 2. 將指南針轉換爲css編程
- 3. 指南針在枚舉指南針 - lucene
- 4. 安裝與指南針的指南針
- 5. 如何在指南針
- 6. 如何穩定指南針?
- 7. 指南針文檔教程
- 8. 遠程使用指南針
- 9. 指南針Flexbox
- 10. Android指南針
- 11. 如何在指南針編譯中指定line_comments?
- 12. 節點指南針不是編譯SCSS
- 13. Yeoman/Grunt沒有編譯指南針
- 14. 指南針詹金斯CI編譯
- 15. 使用指南針編譯Sass文件
- 16. 指南針編譯煎茶錯誤
- 17. 錯誤編譯CSS資產指南針
- 18. 咕嚕/指南針從編譯
- 19. 指南針編譯時間過長
- 20. android指南針rotateanimation
- 21. _base.scss和指南針
- 22. Android OpenGL指南針
- 23. 指南針慣性
- 24. 指南針校準
- 25. 指南針和http_images_path
- 26. ipad 2指南針?
- 27. 指南針Lucene的
- 28. 安裝指南針
- 29. 實現指南針
- 30. OpenGL 3.2編程指南?
嗨@ fida1989感謝您的快速回復,但Sensor.TYPE_ORIENTATION不推薦使用...它無關緊要或將崩潰與新的Android版本? – Elsanty
我希望這不會是一個麻煩。無論如何,請檢查你的情況。 – fida1989
我使用你的例子,並向我顯示的值,但它也顯示了負值的方式,例如-24.4785 ...是否正確? – Elsanty