Android 车载的小小指南针

Android 车载的小小指南针

2023年7月7日发(作者:)

Android 车载的小小指南针【源码】

2010-11-12 14:17 by Terry_龙, 797 visits, 网摘, 收藏, 编辑

在Api Demo 里面有一个指南针的例子,路径为cs-》。源码使用的是SensorListener 做为动力感应为指南针传递角度。基于此原理不断的去画VIEW,目前能搜索出来的代码大致也是根据此DEMO做出修改。代码如下:

/*

* Copyright (C) 2007 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* /licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package cs;

import ty;

import t;

import cs.*;

import Listener;

import Manager;

import ;

import r;

import e;

import Clock;

import ; import ;

import ;

public class Compass extends GraphicsActivity {

private static final String TAG = "Compass";

private SensorManager mSensorManager;

private SampleView mView;

private float[] mValues;

private final SensorListener mListener = new SensorListener() {

public void onSensorChanged(int sensor, float[] values) {

if () Log.d(TAG, "sensorChanged (" + values[0] + ", " +

values[1] + ", " + values[2] + ")");

mValues = values;

if (mView != null) {

date();

}

}

public void onAccuracyChanged(int sensor, int accuracy) {

// TODO Auto-generated method stub

}

};

@Override

protected void onCreate(Bundle icicle) {

te(icicle);

mSensorManager = (SensorManager)getSystemService(_SERVICE);

mView = new SampleView(this);

setContentView(mView);

}

@Override

protected void onResume()

{

if () Log.d(TAG, "onResume");

me();

erListener(mListener,

_ORIENTATION,

_DELAY_GAME);

}

@Override

protected void onStop()

{

if () Log.d(TAG, "onStop");

sterListener(mListener);

();

}

private class SampleView extends View {

private Paint mPaint = new Paint();

private Path mPath = new Path();

private boolean mAnimate;

private long mNextTime;

public SampleView(Context context) {

super(context);

// Construct a wedge-shaped path

(0, -50);

(-20, 60);

(0, 50);

(20, 60);

();

}

@Override protected void onDraw(Canvas canvas) {

Paint paint = mPaint; lor();

iAlias(true);

or();

le();

int w = th();

int h = ght();

int cx = w / 2;

int cy = h / 2;

ate(cx, cy);

if (mValues != null) {

(-mValues[0]);

}

th(mPath, mPaint);

}

@Override

protected void onAttachedToWindow() {

mAnimate = true;

chedToWindow();

}

@Override

protected void onDetachedFromWindow() {

mAnimate = false;

chedFromWindow();

}

}

}

运行结果:

根据Api Demo的例子,我们就可以方便的实现自己想要的指南针效果了。下面是根据Api

Demo给出的实现思路自己完成的一个小小指南针,要实现的效果为如下所示:

首先,项目启动时,设置屏幕为横屏显示:

在配置文件下对应的Activity 加入如下代码:

android:screenOrientation="landscape"

接着画View 将指南针的view 参照api Demo 画出来,由于我个人喜欢View 保存相对的独立性,所以都是将View 尽量的标准化,即可以以编程方式引用也可以在Layout 文件中使用XML引用。下面是我指南针View 代码:

package ;

import tream;

import t;

import ;

import Factory;

import ;

import ;

import s;

import uteSet;

import ;

import s.R;

public class CompassView extends View {

private Bitmap[] mBitmapArray = new Bitmap[4];

InputStream is;

private float[] mValues;

int[] mBitmapWidth = new int[4];

int[] mBitmapHeight = new int[4];

public CompassView(Context context) {

this(context, null);

// TODO Auto-generated constructor stub

}

public CompassView(Context context, AttributeSet attrs) {

super(context, attrs);

s opts = new Options();

DecodeBounds = false;

setBitMapArray(context, 0, opts, );

setBitMapArray(context, 1, opts, s_digree);

}

/**

* 设置bitmap数组个下标的值

*

* @param index

* @param opts

* @param resid

*/

private void setBitMapArray(Context context, int index,

s opts, int resid) {

is = ources().openRawResource(resid);

mBitmapArray[index] = Stream(is);

mBitmapWidth[index] = mBitmapArray[index].getWidth();

mBitmapHeight[index] = mBitmapArray[index].getHeight();

mBitmapArray[index + 2] = Stream(is, null, opts);

mBitmapHeight[index + 2] = mBitmapArray[index + 2].getHeight();

mBitmapWidth[index + 2] = mBitmapArray[index + 2].getWidth();

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

(canvas);

Paint paint = new Paint();

iAlias(true);

int w = th();

int h = ght();

int cx = w / 2;

int cy = h / 2;

ate(cx, cy);

drawPictures(canvas, 0);

} private void drawPictures(Canvas canvas, int idDelta) {

if (mValues != null) {

// Log.d(TAG, "mValues[0] = "+ mValues[0]);

(-mValues[0]);

tmap(mBitmapArray[0 + idDelta],

-mBitmapWidth[0 + idDelta] / 2,

-mBitmapHeight[0 + idDelta] / 2, null);

(360 + mValues[0]);

tmap(mBitmapArray[1 + idDelta],

-mBitmapWidth[1 + idDelta] / 2,

-mBitmapHeight[1 + idDelta] / 2, null);

} else {

tmap(mBitmapArray[0 + idDelta],

-mBitmapWidth[0 + idDelta] / 2,

-mBitmapHeight[0 + idDelta] / 2, null);

tmap(mBitmapArray[1 + idDelta],

-mBitmapWidth[1 + idDelta] / 2,

-mBitmapHeight[1 + idDelta] / 2, null);

}

}

//角度值

public void setValue(float[] value) {

s = value;

}

}

通过上面的绘制VIEW就可以在布局中使用:

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:gravity="center">

android:textAppearance="?android:attr/textAppearanceLarge"

android:layout_width="wrap_content" android:layout_height="wrap_content">

android:layout_width="wrap_content" android:layout_height="wrap_content">

对应Activity使用SensorListener为view 传递角度,当然如果是在车载上面可能不是用SensorListener而是使用GPS等另外其他传过来的参数,这个是后话。

package s;

import ty;

import t;

import ces;

import Listener;

import Manager;

import ;

import Manager;

import ew;

import sView;

@SuppressWarnings("deprecation")

public class CompassActivity extends Activity {

private SensorManager mSensorManager;

private CompassView mView;

private String text = "";

private Resources mRes; private TextView tv;

public final SensorListener mListtenr = new SensorListener() {

@Override

public void onSensorChanged(int sensor, float[] values) {

// TODO Auto-generated method stub

synchronized (this) {

ue(values);

if ((values[0] - 0.0f) < 1)

return;

switch ((int) values[0]) {

case 0:

text = t().toString();

break;

case 90:

text = t().toString();

break;

case 180:

text = t().toString();

break;

case 270:

text = t().toString();

break;

default:

int v = (int) values[0];

if (v > 0 && v < 90) {

text = t(_west).toString()

+ " " + v + " 度";

}

if (v > 90 && v < 180) {

text = t(_west).toString()

+ " " + (180 - v) + " 度";

}

if (v > 180 && v < 270) { text = t(_east).toString()

+ " " + (v - 180) + " 度";

}

if (v > 270 && v < 360) {

text = t(_east).toString()

+ " " + (360 - v) + " 度";

}

break;

}

t(text);

if (mView != null) {

date();

}

}

}

@Override

public void onAccuracyChanged(int sensor, int accuracy) {

// TODO Auto-generated method stub

}

};

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

mSensorManager = (SensorManager) getSystemService(_SERVICE);

mRes = ources();

getWindow().setFlags(_KEEP_SCREEN_ON,

_KEEP_SCREEN_ON); setContentView();

findView();

}

void findView() {

tv = (TextView) findViewById();

mView = (CompassView) findViewById(s);

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

me();

erListener(mListtenr,

_ORIENTATION,

_DELAY_GAME);

}

@Override

protected void onStop() {

// TODO Auto-generated method stub

sterListener(mListtenr);

();

}

}

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688676702a161741.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信