Advertisement
ItsMeLucifer

MAM lab1 main

Oct 9th, 2022 (edited)
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.38 KB | Software | 0 0
  1. -----------------------Main_Acitvity.kt
  2. class MainActivity : AppCompatActivity(), SensorEventListener {
  3.  
  4.     private var currentDegree = 0f
  5.     private var mSensorManager: SensorManager? = null
  6.     private var accelerometer: Sensor? = null
  7.     private var magnetometer: Sensor? = null
  8.     private var orientation: Sensor? = null
  9.     private var mGravity: FloatArray? = null
  10.     private var mGeomagnetic: FloatArray? = null
  11.     override fun onCreate(savedInstanceState: Bundle?) {
  12.         super.onCreate(savedInstanceState)
  13.         setContentView(R.layout.activity_main)
  14.         initData()
  15.     }
  16.     private fun initData(){
  17.         mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager?
  18.         accelerometer = mSensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
  19.         magnetometer = mSensorManager?.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
  20.     }
  21.  
  22.     override fun onResume() {
  23.         super.onResume()
  24.         mSensorManager?.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME)
  25.         mSensorManager?.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_GAME)
  26.         mGravity = floatArrayOf()
  27.         mGeomagnetic = floatArrayOf()
  28.     }
  29.     override fun onSensorChanged(event: SensorEvent?) {
  30.  
  31.         if(event?.sensor?.type == Sensor.TYPE_ACCELEROMETER){
  32.             mGravity = event.values
  33.             val accelerometerText = findViewById<TextView>(R.id.accelerometerText)
  34.             accelerometerText.text = '('+(Math.round((event.values?.get(0)!!) * 10).toDouble()/10).toString()+','+(Math.round((event.values?.get(1)!!) * 10).toDouble()/10).toString()+','+(Math.round((event.values?.get(2)!!) * 10).toDouble()/10).toString()+')'
  35.             val hexText = findViewById<TextView>(R.id.hexText)
  36.             val hex = String.format("#%02x%02x%02x", abs(((((event.values?.get(0)!!) * 10).toDouble()/10)*20).toInt()), abs(((((event.values?.get(1)!!) * 10).toDouble()/10)*20).toInt()), abs(((((event.values?.get(0)!!) * 10).toDouble()/10)*20).toInt()))
  37.             hexText.text = hex.toString()
  38.             val view = findViewById<View>(R.id.viewColor)
  39.             view.setBackgroundColor(Color.parseColor(hex))
  40.         }
  41.  
  42.         if(event?.sensor?.type == Sensor.TYPE_MAGNETIC_FIELD){
  43.             mGeomagnetic = event.values
  44.         }
  45.  
  46.         if (mGravity!!.isNotEmpty() && mGeomagnetic!!.isNotEmpty()) {
  47.             val r = FloatArray(9)
  48.             val i = FloatArray(9)
  49.             val success = SensorManager.getRotationMatrix(r, i, mGravity, mGeomagnetic)
  50.             if (success) {
  51.                 val orientation = FloatArray(3) // orientation contains: azimuth, pitch and roll
  52.                 SensorManager.getOrientation(r, orientation)
  53.                 val orientationText = findViewById<TextView>(R.id.orientationText)
  54.                 orientationText.text = ((Math.toDegrees( orientation[0].toDouble() ) + 360 ) % 360).roundToInt().toString()+'°'
  55.  
  56.             }
  57.         }
  58.  
  59.  
  60.     }
  61.  
  62.     override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
  63.     }
  64.  
  65.     override fun onPause() {
  66.         super.onPause()
  67.         mSensorManager!!.unregisterListener(this)
  68.     }
  69. }
  70. -------------------------Activity_main.xml
  71. <?xml version="1.0" encoding="utf-8"?>
  72. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  73.     xmlns:app="http://schemas.android.com/apk/res-auto"
  74.     xmlns:tools="http://schemas.android.com/tools"
  75.     android:layout_width="match_parent"
  76.     android:layout_height="match_parent"
  77.     tools:context=".MainActivity">
  78.     <TextView
  79.         android:layout_width="wrap_content"
  80.         android:layout_height="wrap_content"
  81.         android:text="Akcelelometr"
  82.         app:layout_constraintBottom_toBottomOf="parent"
  83.         app:layout_constraintHorizontal_bias="0.131"
  84.         app:layout_constraintLeft_toLeftOf="parent"
  85.         app:layout_constraintRight_toRightOf="parent"
  86.         app:layout_constraintTop_toTopOf="parent"
  87.         app:layout_constraintVertical_bias="0.056" />
  88.  
  89.     <TextView
  90.         android:id="@+id/accelerometerText"
  91.         android:layout_width="wrap_content"
  92.         android:layout_height="wrap_content"
  93.         android:text="0.0"
  94.         app:layout_constraintBottom_toBottomOf="parent"
  95.         app:layout_constraintHorizontal_bias="0.194"
  96.         app:layout_constraintLeft_toLeftOf="parent"
  97.         app:layout_constraintRight_toRightOf="parent"
  98.         app:layout_constraintTop_toTopOf="parent"
  99.         app:layout_constraintVertical_bias="0.094" />
  100.  
  101.     <TextView
  102.         android:layout_width="wrap_content"
  103.         android:layout_height="wrap_content"
  104.         android:text="Orientacja"
  105.         app:layout_constraintBottom_toBottomOf="parent"
  106.         app:layout_constraintHorizontal_bias="0.832"
  107.         app:layout_constraintLeft_toLeftOf="parent"
  108.         app:layout_constraintRight_toRightOf="parent"
  109.         app:layout_constraintTop_toTopOf="parent"
  110.         app:layout_constraintVertical_bias="0.056" />
  111.  
  112.     <TextView
  113.         android:id="@+id/orientationText"
  114.         android:layout_width="wrap_content"
  115.         android:layout_height="wrap_content"
  116.         android:text="0.0"
  117.         app:layout_constraintBottom_toBottomOf="parent"
  118.         app:layout_constraintHorizontal_bias="0.797"
  119.         app:layout_constraintLeft_toLeftOf="parent"
  120.         app:layout_constraintRight_toRightOf="parent"
  121.         app:layout_constraintTop_toTopOf="parent"
  122.         app:layout_constraintVertical_bias="0.094" />
  123.  
  124.     <TextView
  125.         android:id="@+id/hexText"
  126.         android:layout_width="wrap_content"
  127.         android:layout_height="wrap_content"
  128.         android:text="#e7eecc"
  129.         app:layout_constraintBottom_toBottomOf="parent"
  130.         app:layout_constraintHorizontal_bias="0.498"
  131.         app:layout_constraintLeft_toLeftOf="parent"
  132.         app:layout_constraintRight_toRightOf="parent"
  133.         app:layout_constraintTop_toTopOf="parent"
  134.         app:layout_constraintVertical_bias="0.198" />
  135.  
  136.     <View android:id="@+id/viewColor"
  137.             android:layout_width="0dp"
  138.             android:layout_height="0dp"
  139.         app:layout_constraintTop_toTopOf="parent"
  140.         app:layout_constraintBottom_toBottomOf="parent"
  141.         app:layout_constraintRight_toRightOf="parent"
  142.         app:layout_constraintLeft_toLeftOf="parent"
  143.         app:layout_constraintDimensionRatio="10:10"
  144.  
  145.         />
  146.  
  147. </androidx.constraintlayout.widget.ConstraintLayout>
Tags: Android Kotlin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement