最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

輕松實現Android指南針功能

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

本文實例為大家講解如何輕松實現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軟件編程有所幫助。

相關文章

最新評論

连江县| 中山市| 原阳县| 象州县| 醴陵市| 巫溪县| 雷山县| 成武县| 盖州市| 庆城县| 石门县| 石台县| 响水县| 丁青县| 菏泽市| 蒲城县| 永春县| 昌吉市| 贵溪市| 西吉县| 福州市| 响水县| 通江县| 黔西| 平陆县| 大庆市| 武城县| 鄢陵县| 高碑店市| 新乡市| 福贡县| 嘉义市| 黔江区| 龙里县| 禹城市| 揭东县| 泸溪县| 宜良县| 济宁市| 沾化县| 宁城县|