自制手机指南针
2015-10-07 00:41
211 查看
[size=medium]
有时候在旅游时候不知道方向了~在小伙伴们面前掏出手机,秀一下我们自制的指南针可是很溜很溜的哟~~~虽然这不是很复杂难懂的原理和技术。下面直接进入代码咯(Just show you my code...)
主函数页面的代码:
下边是页面的布局:
这是图片:
[/size]
有时候在旅游时候不知道方向了~在小伙伴们面前掏出手机,秀一下我们自制的指南针可是很溜很溜的哟~~~虽然这不是很复杂难懂的原理和技术。下面直接进入代码咯(Just show you my code...)
主函数页面的代码:
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;
}
}
}
下边是页面的布局:
<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>
这是图片:
[/size]
相关文章推荐
- 自制手机指南针
- 自制垃圾批处理软件
- 脚本开发工具—快速自制TC防破解网络验证注册码系统
- 手把手教你自制一寸两寸照
- c++自制Redis数据库(三)初步基础框架设计
- 自制ichartjs饼图
- EVC4 自制SDK与VS2005 冲突
- Android自制图片的三级缓存(代码+讲解)
- 自制反汇编逆向分析工具 迭代第四版本
- 驱动程序调试方法之printk——自制proc文件(一)
- java项目开发如何自制java打补丁工具
- java转换文本文件到xlsx(自制缓冲区,无需先验文件行数)
- 自制万能GHOST XP SP3 系统
- 《30天自制操作系统》学习笔记(二)
- 《30天自制操作系统》学习笔记(五)
- unity自制延迟定时回调
- NGUI自制简易虚拟摇杆
- 自制tunnel口建虚拟专网实验
- unity使用ugui自制调色面板
- 自制ZigBee协议分析仪