-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhujiang2
committed
Aug 19, 2021
1 parent
5d1440a
commit f9fd5b7
Showing
11 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MainViewModel>() | ||
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Float> = _xState | ||
|
||
fun onxStateChanged(position: Float) { | ||
_xState.value = position | ||
} | ||
|
||
private val _yState = MutableLiveData(0f) | ||
val yState: LiveData<Float> = _yState | ||
|
||
fun onyStateChanged(position: Float) { | ||
_yState.value = position | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "", | ||
) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters