淺析Android手機衛(wèi)士之抖動輸入框和手機震動
查看apiDemos,找到View/Animation/shake找到對應的動畫代碼,直接拷貝過來
當導入一個項目的時候,報R文件不存在,很多情況是xml文件出錯了
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
et_phone.startAnimation(shake);
動畫的xml文件shake.xml
android:interpolator="@anim/cycle_7"
interpolator是插入器,可以定義動畫的速度等
調(diào)用Animation對象的setInterpolator()方法,設置插入器,參數(shù):Interpolator對象
匿名實現(xiàn)Interpolator接口,重寫getInterpolation()方法,設置中自定義動畫速率,傳入一個flaot x
輸入框的震動效果
獲取Vibrator對象,調(diào)用getSystemService()方法,參數(shù):VIBRATOR_SERVICE
調(diào)用Vibrator對象的vibrate()方法,參數(shù):毫秒
需要添加權限android.permission.VIBRATE
這個可以做一些振動器~
/** * 查詢歸屬地 */ public void queryNumber(View v) { phone = et_phone.getText().toString().trim(); if (TextUtils.isEmpty(phone)) { //抖動動畫 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); et_phone.startAnimation(shake); //手機震動 vibrator.vibrate(2000); Toast.makeText(this, "請輸入手機號碼", 0).show(); return; } String result = NumberQueryAddressUtil.queryAddress(phone); tv_address.setText(result); }
shake.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXDelta="0" android:interpolator="@anim/cycle_7" android:toXDelta="10" />
cycle_7.xml
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />
以上所述是小編給大家介紹的Android手機衛(wèi)士之輸入框抖動和手機震動的相關內(nèi)容,希望對大家有所幫助!
相關文章
Android編程實現(xiàn)webview執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實現(xiàn)webview執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround效果,較為詳細的分析了執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround具體步驟與兩種實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android Studio 3.1.3升級至3.6.1后舊項目的兼容操作方法
這篇文章主要介紹了Android Studio 3.1.3升級至3.6.1后舊項目的兼容操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03