Compass in Android Tutorial


package com.example.deep.fcmnewsapp;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CompassActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorService;
float cd = 0;
ImageView im;
TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);
im = findViewById(R.id.imagedata);
tv = findViewById(R.id.textdata);
sensorService = (SensorManager) getSystemService(SENSOR_SERVICE);
}

@Override
protected void onResume() {
super.onResume();
sensorService.registerListener(this, sensorService.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
super.onPause();
sensorService.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float data = Math.round(sensorEvent.values[0]);
tv.setText(String.valueOf(data));
RotateAnimation ra = new RotateAnimation(cd, -data,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
ra.setDuration(210);
ra.setFillAfter(true);
im.startAnimation(ra);
cd = -data;
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}

}

___________________________________________________________________________

< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
< ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:src="@drawable/com"
android:id="@+id/imagedata"/>
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textSize="20sp"
android:textColor="#000"
android:id="@+id/textdata"
android:gravity="center"
android:textStyle="bold"
android:layout_marginBottom="20dp"
android:text="Current Degree"/>
< /RelativeLayout>
______________________________________________________________________________

< ?xml version="1.0" encoding="utf-8"?>
package="com.example.deep.fcmnewsapp">
< uses-permission-sdk-23 android:name="android.permission.INTERNET" />
< application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
< activity
android:name=".CompassActivity"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< /intent-filter>
< /activity>
< /application>



< /manifest>
_______________________________________________________________________________

For Video Click Here 

No comments: