欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

輕松實現(xiàn)Android指南針功能

 更新時間:2015年12月11日 14:29:59   作者:徐劉根  
這篇文章主要介紹了輕松實現(xiàn)Android指南針功能的幾個關(guān)鍵步驟,想要實現(xiàn)指南針功能的朋友不要錯過

本文實例為大家講解如何輕松實現(xiàn)Android指南針功能,分享給大家供大家參考。具體如下:

(1)布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:orientation="vertical" >

  <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/zn" />

</LinearLayout>

所需圖片:

(2)MainActivity.java

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.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
  private ImageView imageView;
  private SensorManager manager;
  private SensorListener listener = new SensorListener();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imageView = (ImageView) this.findViewById(R.id.imageView);
    imageView.setKeepScreenOn(true);
    manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  }

  @Override
  protected void onResume() {
    Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    manager.registerListener(listener, sensor,
        SensorManager.SENSOR_DELAY_GAME);
    super.onResume();
  }

  @Override
  protected void onPause() {
    manager.unregisterListener(listener);
    super.onPause();
  }

  private final class SensorListener implements SensorEventListener {
    private float predegree = 0;

    public void onSensorChanged(SensorEvent event) {
      float degree = event.values[0];// 存放了方向值 90
      RotateAnimation animation = new RotateAnimation(predegree, -degree,
          Animation.RELATIVE_TO_SELF, 0.5f,
          Animation.RELATIVE_TO_SELF, 0.5f);
      animation.setDuration(200);
      imageView.startAnimation(animation);
      predegree = -degree;
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
  }

}

效果如下:


希望本文所述對大家學習Android軟件編程有所幫助。

相關(guān)文章

  • Android 滑動定位和吸附懸停效果實現(xiàn)代碼

    Android 滑動定位和吸附懸停效果實現(xiàn)代碼

    這篇文章主要介紹了Android 滑動定位和吸附懸停效果實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Android實現(xiàn)微信朋友圈評論EditText效果

    Android實現(xiàn)微信朋友圈評論EditText效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)微信朋友圈評論EditText效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android中操作SQLite數(shù)據(jù)庫快速入門教程

    Android中操作SQLite數(shù)據(jù)庫快速入門教程

    這篇文章主要介紹了Android中操作SQLite數(shù)據(jù)庫快速入門教程,本文講解了數(shù)據(jù)庫基礎(chǔ)概念、Android平臺下數(shù)據(jù)庫相關(guān)類、創(chuàng)建數(shù)據(jù)庫、向表格中添加數(shù)據(jù)、從表格中查詢記錄等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • 一文詳解Android無需權(quán)限調(diào)用系統(tǒng)相機拍照

    一文詳解Android無需權(quán)限調(diào)用系統(tǒng)相機拍照

    這篇文章主要為大家介紹了Android無需權(quán)限調(diào)用系統(tǒng)相機拍照詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 使用Android原生WebView+Highcharts實現(xiàn)可左右滑動的折線圖

    使用Android原生WebView+Highcharts實現(xiàn)可左右滑動的折線圖

    折線圖是Android開發(fā)中經(jīng)常會碰到的效果,但由于涉及自定義View的知識,對許多剛?cè)腴T的小白來說會覺得很高深,下面這篇文章主要給大家介紹了關(guān)于如何使用Android原生WebView+Highcharts實現(xiàn)可左右滑動的折線圖的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Android程序美化之自定義ListView背景的方法

    Android程序美化之自定義ListView背景的方法

    這篇文章主要介紹了Android自定義ListView背景的方法,結(jié)合實例形式分析了ListView控件背景操作方法及常見問題處理技巧,需要的朋友可以參考下
    2016-06-06
  • Ubuntu Android源碼以及內(nèi)核下載與編譯

    Ubuntu Android源碼以及內(nèi)核下載與編譯

    本文主要介紹Android源碼的下載和編譯,這里整理了相關(guān)資料及如何下載和編譯的詳細步驟,有需要的小伙伴可以參考下
    2016-09-09
  • Android自定義樣式圓角dialog對話框

    Android自定義樣式圓角dialog對話框

    這篇文章主要為大家詳細介紹了Android自定義樣式圓角dialog對話框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android Handler工作原理解析

    Android Handler工作原理解析

    這篇文章主要為大家詳細介紹了Android Handler的原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android自定義RecyclerView實現(xiàn)不固定刻度的刻度尺

    Android自定義RecyclerView實現(xiàn)不固定刻度的刻度尺

    這篇文章主要為大家詳細介紹了Android自定義RecyclerView實現(xiàn)不固定刻度的刻度尺,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07

最新評論