Приложение “Уровень”

MainActivity

import android.content.Context
import android.graphics.Color
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView

class MainActivity : AppCompatActivity() {
lateinit var sManager: SensorManager
private var magnetic = FloatArray(9)
private var gravity = FloatArray(9)

private var accrs = FloatArray(3)
private var magf = FloatArray(3)
private var values = FloatArray(3)


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tvSensor = findViewById<TextView>(R.id.tvSensor)
val lRotation = findViewById<LinearLayout>(R.id.lRotation)

sManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
val sensor2 = sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
val sListener = object : SensorEventListener{
override fun onSensorChanged(event: SensorEvent?) {
when(event?.sensor?.type){
Sensor.TYPE_ACCELEROMETER-> accrs = event.values.clone()
Sensor.TYPE_MAGNETIC_FIELD-> magf = event.values.clone()
}

SensorManager.getRotationMatrix(gravity, magnetic, accrs, magf)
val outGravity = FloatArray(9)
SensorManager.remapCoordinateSystem(gravity,
SensorManager.AXIS_X,
SensorManager.AXIS_Z,
outGravity
)
SensorManager.getOrientation(outGravity, values)
val degree = values[2] * 57.2958f
val rotate = 270 + degree
lRotation.rotation = rotate
val rData = 90 + degree
val color = if(rData.toInt() == 0){
Color.GREEN
} else {
Color.RED
}
lRotation.setBackgroundColor(color)
tvSensor.text = rData.toInt().toString()

}

override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

}

}
sManager.registerListener(sListener, sensor, SensorManager.SENSOR_DELAY_NORMAL)
sManager.registerListener(sListener, sensor2, SensorManager.SENSOR_DELAY_NORMAL)
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="@color/black"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvSensor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="90"
android:textColor="@color/white"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.499"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.084" />

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="60dp"
android:layout_height="4dp"
android:background="@color/green"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>

<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="60dp"
android:layout_height="4dp"
android:background="@color/green"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>

<LinearLayout
android:id="@+id/lRotation"
android:layout_width="0dp"
android:layout_height="3dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="@color/green"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/linearLayout3"
app:layout_constraintEnd_toStartOf="@+id/linearLayout4"
app:layout_constraintStart_toEndOf="@+id/linearLayout3"
app:layout_constraintTop_toTopOf="@+id/linearLayout3"></LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AccelerationTestApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="green">#07C500</color>
</resources>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *