标签:
由于真机比较烂,获取所有可用传感器之后发现没有方向传感器和磁场传感器,所以夭折,因此没有进行完全开发,但是根据文档的一些东西添加了使用Sensor的流程,
首先在CustomView中设置一个Getter,就是获取rotateAngle:
public void setRotateAngle(float rotateAngle) { this.rotateAngle = rotateAngle; invalidate(); }
这样我们就可以通过外部调用view的这个方法来设置旋转:
在MainActivity中设置传感器:
1 package com.example.androidviewtest; 2 3 import android.app.Activity; 4 import android.hardware.Sensor; 5 import android.hardware.SensorEvent; 6 import android.hardware.SensorEventListener; 7 import android.hardware.SensorManager; 8 import android.os.Bundle; 9 10 import com.example.androidviewtest.view.CustomView; 11 12 13 public class MainActivity extends Activity implements SensorEventListener{ 14 15 private SensorManager mSensorManager; 16 private Sensor mSensor; 17 18 CustomView view1; 19 CustomView view2; 20 CustomView view3; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 view1 = (CustomView)findViewById(R.id.view1); 28 view2 = (CustomView)findViewById(R.id.view2); 29 view3 = (CustomView)findViewById(R.id.view3); 30 31 mSensorManager =(SensorManager)getSystemService(SENSOR_SERVICE); 32 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 33 } 34 35 @Override 36 protected void onStart() { 37 super.onStart(); 38 mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_UI); 39 } 40 41 @Override 42 protected void onPause() { 43 super.onPause(); 44 mSensorManager.unregisterListener(this); 45 } 46 47 @Override 48 public void onSensorChanged(SensorEvent event) { 49 float[] values = event.values; 50 view1.setRotateAngle(values[0]); 51 view2.setRotateAngle(values[1]); 52 view2.setRotateAngle(values[2]); 53 } 54 55 @Override 56 public void onAccuracyChanged(Sensor sensor, int accuracy) { 57 58 } 59 }
我就不再继续这个View的学习了,下阶段了解CircleImageView的使用和实现。
标签:
原文地址:http://www.cnblogs.com/lhyz/p/4432859.html