淺析Android手機(jī)衛(wèi)士之抖動(dòng)輸入框和手機(jī)震動(dòng)
查看apiDemos,找到View/Animation/shake找到對(duì)應(yīng)的動(dòng)畫代碼,直接拷貝過來
當(dāng)導(dǎo)入一個(gè)項(xiàng)目的時(shí)候,報(bào)R文件不存在,很多情況是xml文件出錯(cuò)了
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
et_phone.startAnimation(shake);
動(dòng)畫的xml文件shake.xml
android:interpolator="@anim/cycle_7"
interpolator是插入器,可以定義動(dòng)畫的速度等
調(diào)用Animation對(duì)象的setInterpolator()方法,設(shè)置插入器,參數(shù):Interpolator對(duì)象
匿名實(shí)現(xiàn)Interpolator接口,重寫getInterpolation()方法,設(shè)置中自定義動(dòng)畫速率,傳入一個(gè)flaot x
輸入框的震動(dòng)效果
獲取Vibrator對(duì)象,調(diào)用getSystemService()方法,參數(shù):VIBRATOR_SERVICE
調(diào)用Vibrator對(duì)象的vibrate()方法,參數(shù):毫秒
需要添加權(quán)限android.permission.VIBRATE
這個(gè)可以做一些振動(dòng)器~
/** * 查詢歸屬地 */ public void queryNumber(View v) { phone = et_phone.getText().toString().trim(); if (TextUtils.isEmpty(phone)) { //抖動(dòng)動(dòng)畫 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); et_phone.startAnimation(shake); //手機(jī)震動(dòng) vibrator.vibrate(2000); Toast.makeText(this, "請(qǐng)輸入手機(jī)號(hào)碼", 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手機(jī)衛(wèi)士之輸入框抖動(dòng)和手機(jī)震動(dòng)的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!
相關(guān)文章
Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果,較為詳細(xì)的分析了執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround具體步驟與兩種實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android入門之計(jì)時(shí)器Chronometer的使用教程
Chronometer是一個(gè)簡單的定時(shí)器,你可以給它一個(gè)開始時(shí)間,并以此定時(shí)。本文將利用個(gè)簡單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下2022-11-11Android Studio 3.1.3升級(jí)至3.6.1后舊項(xiàng)目的兼容操作方法
這篇文章主要介紹了Android Studio 3.1.3升級(jí)至3.6.1后舊項(xiàng)目的兼容操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03SimpleCommand實(shí)現(xiàn)圖片下載(二)
這篇文章主要為大家詳細(xì)介紹了SimpleCommand實(shí)現(xiàn)圖片下載,并顯示到ImageView控件上,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android開發(fā)之電話撥號(hào)器實(shí)例詳解
這篇文章主要介紹了Android開發(fā)之電話撥號(hào)器,結(jié)合實(shí)例形式詳細(xì)分析了Android電話撥號(hào)器的實(shí)現(xiàn)步驟與具體代碼,并附帶了一個(gè)Android開放電話撥號(hào)器的學(xué)習(xí)筆記,需要的朋友可以參考下2015-12-12