安卓感應器開發之指南針,安卓感應器指南針

來源:互聯網
上載者:User

安卓感應器開發之指南針,安卓感應器指南針

代碼很簡單,主要是一些常用感應器的監聽,指南針還是挺好用的。


布局代碼

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#fff"><ImageViewandroid:id="@+id/znzImage"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scaleType="fitCenter"android:src="@drawable/compass" /></LinearLayout>



程式碼:

package zhinanzheng.com;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;/** * @author coderwry 653866417@qq.com * @version  1.0 */public class Zhinanzheng extends Activity implements SensorEventListener{ImageView image;  //指南針圖片float currentDegree = 0f; //指南針圖片轉過的角度SensorManager mSensorManager; //管理器    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                image = (ImageView)findViewById(R.id.znzImage);        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); //擷取管理服務    }        @Override     protected void onResume(){    super.onResume();    //註冊監聽器    mSensorManager.registerListener(this    , mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);    }        //取消註冊    @Override    protected void onPause(){    mSensorManager.unregisterListener(this);    super.onPause();        }        @Override    protected void onStop(){    mSensorManager.unregisterListener(this);    super.onStop();        }    //感應器值改變@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}    //精度改變@Overridepublic void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stub//擷取觸發event的感應器類型int sensorType = event.sensor.getType();switch(sensorType){case Sensor.TYPE_ORIENTATION:float degree = event.values[0]; //擷取z轉過的角度//穿件旋轉動畫RotateAnimation ra = new RotateAnimation(currentDegree,-degree,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); ra.setDuration(100);//動畫期間 image.startAnimation(ra); currentDegree = -degree; break;}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.