Android 指南针
2019-10-12 本文已影响0人
残非
MainActivity
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private TextView tv1,tv2,tv3;
private ImageView img;
private SensorManager sensorManager;
private Sensor sensor;
private float startAngle = 0;
private DefinedRotateAnimation definedRotateAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
//https://gitee.com/zhanma/codes/qw28ndj0akcpvf65hl1rg41#%E5%B8%83%E5%B1%80%E6%96%87%E4%BB%B6
tv1 = (TextView)findViewById(R.id.tv1);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
img = (ImageView)findViewById(R.id.img);
//获取传感器管理者,通过系统获取
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
//获取方向传感器
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
definedRotateAnimation = new DefinedRotateAnimation();
definedRotateAnimation.setDuration(200);
definedRotateAnimation.setFillAfter(true);
tv1.bringToFront();
}
@Override
protected void onResume() {
super.onResume();
//第三个参数:传感器刷新的更新速度
sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
float presentAngle = event.values[0];
if(startAngle == (-presentAngle)){
return ;
}
if((presentAngle>=340&&presentAngle<=360)||(presentAngle>=0&&presentAngle<=20)){
tv3.setText("北");
}else if(presentAngle>20&&presentAngle<70){
tv3.setText("东北");
}else if(presentAngle>=70&&presentAngle<=110){
tv3.setText("东");
}else if(presentAngle>110&&presentAngle<160){
tv3.setText("东南");
}else if(presentAngle>=160&&presentAngle<=200){
tv3.setText("南");
}else if(presentAngle>200&presentAngle<250){
tv3.setText("西南");
}else if(presentAngle>=250&&presentAngle<=290){
tv3.setText("西");
}else {
tv3.setText("西北");
}
tv2.setText((int)presentAngle + "°");
presentAngle = (-presentAngle);
definedRotateAnimation.setStartAngle(startAngle);
definedRotateAnimation.setEndAngle(presentAngle);
img.startAnimation(definedRotateAnimation);
startAngle = presentAngle;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
DefinedRotateAnimation(自定义旋转动画)
class DefinedRotateAnimation extends Animation{
private float startAngle = 0;
private float endAngle = 0;
private int width = 0;
private int height = 0;
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
this.width = width;
this.height = height;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float angleDifference = endAngle - startAngle;
//interpolatedTime时间区间【0-1】,逐渐递增
t.getMatrix().setRotate(startAngle + angleDifference * interpolatedTime,width/2,height/2);
}
public float getStartAngle(){
return this.startAngle;
}
public void setStartAngle(float startAngle){
this.startAngle = startAngle;
}
public float getEndAngle(){
return this.endAngle;
}
public void setEndAngle(float endAngle){
this.endAngle = endAngle;
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.zm0903_zhinanzheng.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textSize="40dp"
android:textColor="@color/colorPrimary"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
></TextView>
<ImageView
android:id="@+id/img"
android:layout_width="340dp"
android:layout_height="340dp"
android:background="@drawable/a"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
></ImageView>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="@color/colorPrimaryDark"
android:layout_below="@id/img"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="239"
></TextView>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="@color/colorPrimary"
android:layout_marginTop="3dp"
android:layout_below="@id/tv2"
android:layout_centerHorizontal="true"
android:text="西南 "
></TextView>
</RelativeLayout >