微信搖一搖,抖音搖一搖面對面搖骰子等,手機(jī)APP搖一搖功能隨處可見,下面我們來實(shí)現(xiàn)一個(gè)簡單的搖一搖功能。
常見應(yīng)用場景:
「實(shí)現(xiàn)效果」:在1.5s內(nèi)出現(xiàn)兩次加速度達(dá)到15則觸發(fā)搖一搖
class ShakeManager : SensorEventListener { private var mContext: Context /** * 時(shí)間范圍 */ private var mIntervalTimeMillis: Long = 15000 /** * 要換次數(shù)閾值 */ 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 /** * 上一次搖晃時(shí)間 */ private var mLastShakeTimeMillis: Long = 0 /** * 搖晃次數(shù) */ 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() //搖晃時(shí)間大于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() }}
圖片
接下來搖晃手機(jī),觸發(fā)搖一搖機(jī)制,字體變紅
圖片
完整代碼:
<?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="請搖晃手機(jī)" 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 = "觸發(fā)了搖一搖" mBinding.tvShake.setTextColor(0xFFFF0000.toInt()) } }) }}
隨著技術(shù)發(fā)展,結(jié)合設(shè)備傳感器,可以開發(fā)出更多有趣和實(shí)用的應(yīng)用。
本文鏈接:http://www.tebozhan.com/showinfo-26-60917-0.htmlAndroid應(yīng)用開發(fā)簡單幾步實(shí)現(xiàn)搖一搖功能
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com