Компас на Kotlin: Урок 13

MainActivity.kt

import android.content.Context
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.view.animation.Animation
import android.view.animation.RotateAnimation
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(),SensorEventListener {
var manager:SensorManager? = null
var current_degree:Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
manager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

}

override fun onResume() {
super.onResume()
manager?.registerListener(this,manager?.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME)

}

override fun onPause() {
super.onPause()
manager?.unregisterListener(this)
}

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

}

override fun onSensorChanged(p0: SensorEvent?) {
val degree:Int = p0?.values?.get(0)?.toInt()!!
tvDegree.text = degree.toString()
val rotationAnim = RotateAnimation(current_degree.toFloat(),(-degree).toFloat(),Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f)
rotationAnim.duration = 210
rotationAnim.fillAfter = true
current_degree = -degree
imDinamic.startAnimation(rotationAnim)

}
}

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="#000000"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvDegree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.14" />

<ImageView
android:id="@+id/imStatic"
android:layout_width="0dp"
android:layout_height="350dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvDegree"
app:layout_constraintVertical_bias="0.17000002"
app:srcCompat="@drawable/img_compass_static" />

<ImageView
android:id="@+id/imDinamic"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/imStatic"
app:layout_constraintEnd_toEndOf="@+id/imStatic"
app:layout_constraintStart_toStartOf="@+id/imStatic"
app:layout_constraintTop_toTopOf="@+id/imStatic"
app:srcCompat="@drawable/img_compass_dinamic" />

</androidx.constraintlayout.widget.ConstraintLayout>

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

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