編輯:關於Android編程
package com.home.compass;
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;
public class CompassTestActivity extends Activity implements
SensorEventListener {
// 定義顯示指南針圖片的組件
private ImageView image;
// 記錄指南針圖片轉過的角度
private float currentDegree = 0f;
// 定義真機的Sensor管理器
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.main_iv);
// 獲取真機的傳感器管理服務
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() {
super.onPause();
// 取消注冊
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
// 如果真機上觸發event的傳感器類型為水平傳感器類型
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
// 獲取繞Z軸轉過的角度
float degree = event.values[0];
// 創建旋轉動畫(反向轉過degree度)
RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
// 設置動畫的持續時間
ra.setDuration(200);
// 設置動畫結束後的保留狀態
ra.setFillAfter(true);
// 啟動動畫
image.startAnimation(ra);
currentDegree = -degree;
}
}
}
package com.home.compass;
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;
public class CompassTestActivity extends Activity implements
SensorEventListener {
// 定義顯示指南針圖片的組件
private ImageView image;
// 記錄指南針圖片轉過的角度
private float currentDegree = 0f;
// 定義真機的Sensor管理器
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.main_iv);
// 獲取真機的傳感器管理服務
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() {
super.onPause();
// 取消注冊
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
// 如果真機上觸發event的傳感器類型為水平傳感器類型
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
// 獲取繞Z軸轉過的角度
float degree = event.values[0];
// 創建旋轉動畫(反向轉過degree度)
RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
// 設置動畫的持續時間
ra.setDuration(200);
// 設置動畫結束後的保留狀態
ra.setFillAfter(true);
// 啟動動畫
image.startAnimation(ra);
currentDegree = -degree;
}
}
}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ImageView
android:id="@+id/main_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/znz" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ImageView
android:id="@+id/main_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/znz" />
</LinearLayout>這裡附上一張指南針的圖片:
Android自定義控件系列二:自定義開關按鈕(一)
這一次我們將會實現一個完整純粹的自定義控件,而不是像之前的組合控件一樣,拿系統的控件來實現;計劃分為三部分:自定義控件的基本部分,自定義控件的觸摸事件的處理和自定義控件的
Android跑馬燈MarqueeView源碼解析
跑馬燈效果,大家可以去原作者浏覽https://github.com/sfsheng0322/MarqueeView 下面看自定義控件的代碼public class Ma
AndroidManifest配置文件介紹
本 質:AndroidManifest.xml 是 整 個 應 用 的 主 配 置 清 單 文 件 。 包 含 :該 應 用 的 包 名 、 版 本 號 、 組 件 、
Android popupwindow 示例程序一
經過多番測試實踐,實現了popupwindow 彈出在指定控件的下方。代碼上有注釋,有需要注意的地方。popupwindow 有自已的布局,裡面控件的監聽實現都有。接下來