當你使用手機時,你可能不會注意到它在白天,但當你在晚上安靜的時候,你會發現當你輸入時,屏幕會輕微振動,本文是武林技術頻道小編淺析Android手機衛士之抖動輸入框和手機震動,一起來了解一下!
查看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是插入器,可以定義動畫的速度等
調用Animation對象的setInterpolator()方法,設置插入器,參數:Interpolator對象
匿名實現Interpolator接口,重寫getInterpolation()方法,設置中自定義動畫速率,傳入一個flaot x
輸入框的震動效果
獲取Vibrator對象,調用getSystemService()方法,參數:VIBRATOR_SERVICE
調用Vibrator對象的vibrate()方法,參數:毫秒
需要添加權限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手機衛士之抖動輸入框和手機震動,希望對你了解這方面知識有所幫助,也希望大家能繼續支持js.Vevb.com。
新聞熱點
疑難解答