国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

輕松實現Android指南針功能

2020-04-11 11:12:17
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 水城县| 酉阳| 文化| 冕宁县| 荣成市| 临泉县| 深水埗区| 建昌县| 安远县| 新田县| 邯郸市| 常山县| 太湖县| 文登市| 嘉荫县| 新竹县| 江安县| 深水埗区| 开封县| 冀州市| 孟州市| 岚皋县| 巢湖市| 湖口县| 泸西县| 涞水县| 阿荣旗| 嘉祥县| 利川市| 固始县| 宁陵县| 陵水| 柳州市| 遂宁市| 商水县| 汉中市| 正蓝旗| 泰顺县| 柘荣县| 潢川县| 永康市|