Skip to content

Commit

Permalink
新增3D效果
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujiang2 committed Aug 19, 2021
1 parent 5d1440a commit f9fd5b7
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 15 deletions.
15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/zj/test/BarUtils.kt
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
}
}
73 changes: 68 additions & 5 deletions app/src/main/java/com/zj/test/MainActivity.kt
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)
}

}
23 changes: 23 additions & 0 deletions app/src/main/java/com/zj/test/MainViewModel.kt
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
}

}
32 changes: 32 additions & 0 deletions app/src/main/java/com/zj/test/ThreeDImage.kt
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.
2 changes: 2 additions & 0 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="Theme.Banner.NoActionBar">
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
compose_version = '1.0.0-rc02'
compose_version = '1.0.1'
}
repositories {
google()
Expand All @@ -10,8 +10,8 @@ buildscript {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0-beta05'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
classpath 'com.android.tools.build:gradle:7.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit f9fd5b7

Please sign in to comment.