diff --git a/app/build.gradle b/app/build.gradle index 20449e9..a21dca3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,23 +37,24 @@ android { } composeOptions { kotlinCompilerExtensionVersion compose_version - kotlinCompilerVersion '1.4.31' + kotlinCompilerVersion '1.5.21' } } dependencies { - implementation 'androidx.core:core-ktx:1.5.0' - implementation 'androidx.appcompat:appcompat:1.3.0' - implementation 'com.google.android.material:material:1.3.0' + implementation 'androidx.core:core-ktx:1.6.0' + implementation 'androidx.appcompat:appcompat:1.3.1' + implementation 'com.google.android.material:material:1.4.0' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' - implementation 'androidx.activity:activity-compose:1.3.0-beta01' + implementation 'androidx.activity:activity-compose:1.3.1' + implementation "androidx.compose.runtime:runtime-livedata:$compose_version" //implementation 'com.github.zhujiang521:Banner:1.3.3' implementation project(path: ':banner') testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.2' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" } \ No newline at end of file diff --git a/app/src/main/java/com/zj/test/BarUtils.kt b/app/src/main/java/com/zj/test/BarUtils.kt new file mode 100644 index 0000000..78f723c --- /dev/null +++ b/app/src/main/java/com/zj/test/BarUtils.kt @@ -0,0 +1,39 @@ +package com.zj.test + +import android.app.Activity +import android.graphics.Color +import android.view.View +import android.view.Window +import android.view.WindowManager + +/** + * 设置透明状态栏 + */ +fun Activity.transparentStatusBar() { + transparentStatusBar(window) +} + +private fun transparentStatusBar(window: Window) { + window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) + val option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + val vis = window.decorView.systemUiVisibility + window.decorView.systemUiVisibility = option or vis + window.statusBarColor = Color.TRANSPARENT +} + + +/** + * 状态栏反色 + */ +fun Activity.setAndroidNativeLightStatusBar() { + val decor = window.decorView + val isDark = resources.configuration.uiMode == 0x21 + if (!isDark) { + decor.systemUiVisibility = + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR + } else { + decor.systemUiVisibility = + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE + } +} diff --git a/app/src/main/java/com/zj/test/MainActivity.kt b/app/src/main/java/com/zj/test/MainActivity.kt index bb6bd2a..9643f66 100644 --- a/app/src/main/java/com/zj/test/MainActivity.kt +++ b/app/src/main/java/com/zj/test/MainActivity.kt @@ -1,24 +1,87 @@ package com.zj.test +import android.content.Context +import android.hardware.Sensor +import android.hardware.SensorEvent +import android.hardware.SensorEventListener +import android.hardware.SensorManager import android.os.Bundle +import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.activity.viewModels import androidx.compose.material.MaterialTheme import androidx.compose.material.Surface -import com.google.accompanist.pager.ExperimentalPagerApi +import androidx.compose.runtime.getValue +import androidx.compose.runtime.livedata.observeAsState import com.zj.test.ui.theme.BannerTheme +import kotlin.math.sqrt + +class MainActivity : ComponentActivity(), SensorEventListener { + + companion object { + private const val TAG = "MainActivity" + } + + private val viewModel by viewModels() + private lateinit var mSensorManager: SensorManager + private lateinit var mMagneticSensor: Sensor -class MainActivity : ComponentActivity() { - @ExperimentalPagerApi override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + transparentStatusBar() + setAndroidNativeLightStatusBar() + mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager + // 陀螺仪传感器 + mMagneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) + + mSensorManager.registerListener(this, mMagneticSensor, SensorManager.SENSOR_DELAY_GAME) + setContent { BannerTheme { - // A surface container using the 'background' color from the theme + val xState by viewModel.xState.observeAsState(0f) + val yState by viewModel.yState.observeAsState(0f) Surface(color = MaterialTheme.colors.background) { - BannerTest() + ThreeDImage(xState, yState) } } } } + + override fun onSensorChanged(event: SensorEvent?) { + if (event == null) return + when (event.sensor.type) { + Sensor.TYPE_ACCELEROMETER -> { + // x,y,z分别存储坐标轴x,y,z上的加速度 + val x = event.values[0] + val y = event.values[1] + val z = event.values[2] + refreshState(x, y) + Log.d(TAG, "TYPE_ACCELEROMETER x:$x y:$y z:$z") + } + Sensor.TYPE_MAGNETIC_FIELD -> { + // 三个坐标轴方向上的电磁强度,单位是微特拉斯(micro-Tesla),用uT表示,也可以是高斯(Gauss),1Tesla=10000Gauss + val x = event.values[0] + val y = event.values[1] + val z = event.values[2] + refreshState(x, y) + Log.d(TAG, "TYPE_MAGNETIC_FIELD x:$x y:$y z:$z") + } + } + } + + private fun refreshState(x: Float, y: Float) { + viewModel.onxStateChanged(x) + viewModel.onyStateChanged(y) + } + + override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { + Log.d(TAG, "onAccuracyChanged: accuracy:$accuracy") + } + + override fun onDestroy() { + super.onDestroy() + mSensorManager.unregisterListener(this) + } + } \ No newline at end of file diff --git a/app/src/main/java/com/zj/test/MainViewModel.kt b/app/src/main/java/com/zj/test/MainViewModel.kt new file mode 100644 index 0000000..10bbfa3 --- /dev/null +++ b/app/src/main/java/com/zj/test/MainViewModel.kt @@ -0,0 +1,23 @@ +package com.zj.test + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel + +class MainViewModel : ViewModel() { + + private val _xState = MutableLiveData(0f) + val xState: LiveData = _xState + + fun onxStateChanged(position: Float) { + _xState.value = position + } + + private val _yState = MutableLiveData(0f) + val yState: LiveData = _yState + + fun onyStateChanged(position: Float) { + _yState.value = position + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/zj/test/ThreeDImage.kt b/app/src/main/java/com/zj/test/ThreeDImage.kt new file mode 100644 index 0000000..5ed6610 --- /dev/null +++ b/app/src/main/java/com/zj/test/ThreeDImage.kt @@ -0,0 +1,32 @@ +package com.zj.test + +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.offset +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.unit.dp + + +@Composable +fun ThreeDImage(x: Float, y: Float) { + Box(modifier = Modifier.fillMaxSize()) { + Image( + modifier = Modifier + .fillMaxSize(), + contentScale = ContentScale.Crop, + painter = painterResource(id = R.drawable.icon_three_bg), + contentDescription = "", + ) + Image( + modifier = Modifier + .fillMaxSize() + .offset(x = x.dp, y = y.dp), + painter = painterResource(id = R.drawable.icon_three_small), + contentDescription = "", + ) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-xxhdpi/icon_three_bg.png b/app/src/main/res/drawable-xxhdpi/icon_three_bg.png new file mode 100644 index 0000000..3081f15 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/icon_three_bg.png differ diff --git a/app/src/main/res/drawable-xxhdpi/icon_three_content.png b/app/src/main/res/drawable-xxhdpi/icon_three_content.png new file mode 100644 index 0000000..fcd135f Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/icon_three_content.png differ diff --git a/app/src/main/res/drawable-xxhdpi/icon_three_small.png b/app/src/main/res/drawable-xxhdpi/icon_three_small.png new file mode 100644 index 0000000..624cb03 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/icon_three_small.png differ diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml index ae94313..2956e3b 100644 --- a/app/src/main/res/values-night/themes.xml +++ b/app/src/main/res/values-night/themes.xml @@ -12,5 +12,7 @@ ?attr/colorPrimaryVariant + false + true \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index c8a718a..8f5f0bb 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -12,6 +12,8 @@ ?attr/colorPrimaryVariant + false + true