Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------Main_Acitvity.kt
- class MainActivity : AppCompatActivity(), SensorEventListener {
- private var currentDegree = 0f
- private var mSensorManager: SensorManager? = null
- private var accelerometer: Sensor? = null
- private var magnetometer: Sensor? = null
- private var orientation: Sensor? = null
- private var mGravity: FloatArray? = null
- private var mGeomagnetic: FloatArray? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- initData()
- }
- private fun initData(){
- mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager?
- accelerometer = mSensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
- magnetometer = mSensorManager?.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
- }
- override fun onResume() {
- super.onResume()
- mSensorManager?.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME)
- mSensorManager?.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_GAME)
- mGravity = floatArrayOf()
- mGeomagnetic = floatArrayOf()
- }
- override fun onSensorChanged(event: SensorEvent?) {
- if(event?.sensor?.type == Sensor.TYPE_ACCELEROMETER){
- mGravity = event.values
- val accelerometerText = findViewById<TextView>(R.id.accelerometerText)
- 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()+')'
- val hexText = findViewById<TextView>(R.id.hexText)
- 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()))
- hexText.text = hex.toString()
- val view = findViewById<View>(R.id.viewColor)
- view.setBackgroundColor(Color.parseColor(hex))
- }
- if(event?.sensor?.type == Sensor.TYPE_MAGNETIC_FIELD){
- mGeomagnetic = event.values
- }
- if (mGravity!!.isNotEmpty() && mGeomagnetic!!.isNotEmpty()) {
- val r = FloatArray(9)
- val i = FloatArray(9)
- val success = SensorManager.getRotationMatrix(r, i, mGravity, mGeomagnetic)
- if (success) {
- val orientation = FloatArray(3) // orientation contains: azimuth, pitch and roll
- SensorManager.getOrientation(r, orientation)
- val orientationText = findViewById<TextView>(R.id.orientationText)
- orientationText.text = ((Math.toDegrees( orientation[0].toDouble() ) + 360 ) % 360).roundToInt().toString()+'°'
- }
- }
- }
- override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
- }
- override fun onPause() {
- super.onPause()
- mSensorManager!!.unregisterListener(this)
- }
- }
- -------------------------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"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Akcelelometr"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintHorizontal_bias="0.131"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.056" />
- <TextView
- android:id="@+id/accelerometerText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0.0"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintHorizontal_bias="0.194"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.094" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Orientacja"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintHorizontal_bias="0.832"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.056" />
- <TextView
- android:id="@+id/orientationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0.0"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintHorizontal_bias="0.797"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.094" />
- <TextView
- android:id="@+id/hexText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="#e7eecc"
- 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.198" />
- <View android:id="@+id/viewColor"
- android:layout_width="0dp"
- android:layout_height="0dp"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintDimensionRatio="10:10"
- />
- </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement