android应用示例代码
今天,我将分享一个有效的代码,为您的android设备制作一个非常简单的罗盘应用程序。
某些Android设备(例如Huawei Y300和Lenovo P700i)不完全支持运动传感器,因此该代码不适用于它们。
影片示范
我们今天的代码将像这样运行:
所需文件
您需要创建自己的指南针图像。 在此示例中,我使用的是照片。 您的图片必须是透明背景的PNG,请勿使用我使用的jpg文件。
让我们编码
这是我们的MainActivity.java
package passapp;import android.app.Activity;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity implements SensorEventListener {// define the display assembly compass pictureprivate ImageView image;// record the compass picture angle turnedprivate float currentDegree = 0f;// device sensor managerprivate SensorManager mSensorManager;TextView tvHeading;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// image = (ImageView) findViewById(R.id.main_iv);// TextView that will tell the user what degree is he headingtvHeading = (TextView) findViewById(R.id.tvHeading);// initialize your android device sensor capabilitiesmSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);}@Overrideprotected void onResume() {super.onResume();// for the system's orientation sensor registered listenersmSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onPause() {super.onPause();// to stop the listener and save batterymSensorManager.unregisterListener(this);}@Overridepublic void onSensorChanged(SensorEvent event) {// get the angle around the z-axis rotatedfloat degree = Math.round(event.values[0]);tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");// create a rotation animation (reverse turn degree degrees)RotateAnimation ra = new RotateAnimation(currentDegree, -degree,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);// how long the animation will take placera.setDuration(210);// set the animation after the end of the reservation statusra.setFillAfter(true);// Start the animationimage.startAnimation(ra);currentDegree = -degree;}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// not in use}}
我们的布局文件activity_main.xml
<RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fff" ><TextViewandroid:id="@+id/tvHeading"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginBottom="40dp"android:layout_marginTop="20dp"android:text="Heading: 0.0" /><ImageViewandroid:id="@+id/imageViewCompass"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/tvHeading"android:layout_centerHorizontal="true"android:src="@drawable/img_compass" /></RelativeLayout>
源代码下载
您可以在此处下载此示例项目: CompassApp.zip
一些注意事项
我的应用方向锁定为纵向模式。 清单文件中没有特殊权限。
进一步阅读
Android SensorManager Android RotateAnimation Android运动传感器 参考:《 The Ninja博客》中来自JCG合作伙伴 Mike Dalisay的Android Compass代码示例 。翻译自: //09/android-compass-code-example.html
android应用示例代码