查看apiDemos,找到View/Animation/shake找到對應的動畫代碼,直接拷貝過來 當導入一個項目的時候,報R文件不存在,很多情況是xml文件出錯了 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); ...
查看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" />