官方承諾:K60至尊版將會首批升級MIUI 15
全新的MIUI 15今天也有了消息,在官宣了K60至尊版將會搭載天璣9200+處理器和獨顯芯片X7的同時,Redmi給出了官方承諾,K60至尊重大更新首批升級,會首批推送MIUI 15。也就是說雖然
微信搖一搖,抖音搖一搖面對面搖骰子等,手機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.www897cc.com/showinfo-26-60917-0.htmlAndroid應用開發簡單幾步實現搖一搖功能
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Java、Spring和Dubbo三種SPI機制,到底誰更好?
下一篇: 現學現用,寫個Maven插件用下