感想:以前学习web的时候老师就跟我们说要有自己的博客,直到今天,突然意识到它的重要性,所以觉得从今天开始,对于自己技术上的解决的问题,没有解决的问题都放入自己的博客中,这样一个是可以记录自己的学习历程,以后也可以查看,同时还可以分享到网络上,也许能够帮助到哪些能帮助到的人。以后希望自己能够坚持。。。不管是学习还是以后工作啦!
同时也真心的希望不管是技术菜鸟还是技术牛人都能也有自己的博客或者其他的分享平台,说得伟大点是为中国IT事业做出自己的贡献。
代码其实很简单,但是手机必须支持方向传感器。最后的效果:
activity中部分代码如下:
public class MainActivity extends Activity {
ImageView iv;
SensorManager manager;
float currentDegree=0.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv);
manager=(SensorManager) getSystemService(SENSOR_SERVICE);
manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
}
SensorEventListener listener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float degree=event.values[0];
//用RotateAnimation 来实现指南针的变动
RotateAnimation ra=new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
//设置动画持续时间
ra.setDuration(200);
iv.startAnimation(ra);
currentDegree=-degree;
}
xml文件内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="@string/cd"
android:src="@drawable/znz" />
</RelativeLayout>