微信摇一摇,抖音摇一摇面对面摇骰子等,手机APP摇一摇功能随处可见,下面我们来实现一个简单的摇一摇功能。
常见应用场景:
「实现效果」:在1.5s内出现两次加速度达到15则触发摇一摇
class ShakeManager : SensorEventListener { private var mContext: Context /** * 时间范围 */ private var mIntervalTimeMillis: Long = 15000 /** * 要换次数阈值 */ private var mThresholdCount: Int = 2 /** * 加速度阈值 */ private var mShakeThreshold: Int = 15 private var mSensorManager: SensorManager? = null private var mVibrator: Vibrator? = null private var mOnShakeListener: OnShakeListener? = null /** * 上一次摇晃时间 */ private var mLastShakeTimeMillis: Long = 0 /** * 摇晃次数 */ private var mShakeCount = 0 constructor( context: Context, intervalTimeMillis: Long, thresholdCount: Int, shakeThreshold: Int, onShakeListener: OnShakeListener ) { this.mContext = context this.mIntervalTimeMillis = intervalTimeMillis this.mThresholdCount = thresholdCount this.mShakeThreshold = shakeThreshold this.mOnShakeListener = onShakeListener mSensorManager = mContext.getSystemService(Context.SENSOR_SERVICE) as SensorManager mVibrator = mContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator mSensorManager?.registerListener( this, mSensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL ) } override fun onSensorChanged(event: SensorEvent) { //加速度变化 if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) { val currentTimeMillis = System.currentTimeMillis() //摇晃时间大于1500 if (currentTimeMillis - mLastShakeTimeMillis > mIntervalTimeMillis) { mShakeCount = 0 } val values = event.values if (abs(values[0]) > mShakeThreshold || abs(values[1]) > mShakeThreshold || abs(values[2]) > mShakeThreshold) { mLastShakeTimeMillis = currentTimeMillis mShakeCount += 1 if (mShakeCount > mThresholdCount) { mVibrator?.vibrate(100) mShakeCount = 0 mLastShakeTimeMillis = 0 mOnShakeListener?.onShaked() } } } } override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { } interface OnShakeListener { fun onShaked() }}
图片
接下来摇晃手机,触发摇一摇机制,字体变红
图片
完整代码:
<?xml versinotallow="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".activity.TestActivity"> <TextView android:id="@+id/tv_shake" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="请摇晃手机" android:textColor="@color/black" android:textSize="20sp" /></LinearLayout>
class TestActivity : AppCompatActivity() { private val TAG = TestActivity::class.java.simpleName private lateinit var mBinding: ActivityTestBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mBinding = ActivityTestBinding.inflate(layoutInflater) setContentView(mBinding.root) ShakeManager(this, 1500, 2, 15, object : ShakeManager.OnShakeListener { override fun onShaked() { mBinding.tvShake.text = "触发了摇一摇" mBinding.tvShake.setTextColor(0xFFFF0000.toInt()) } }) }}
随着技术发展,结合设备传感器,可以开发出更多有趣和实用的应用。
本文链接:http://www.28at.com/showinfo-26-60917-0.htmlAndroid应用开发简单几步实现摇一摇功能
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com
上一篇: Java、Spring和Dubbo三种SPI机制,到底谁更好?
下一篇: 现学现用,写个Maven插件用下