[android] 手機衛士輸入框抖動和手機震動

來源:互聯網
上載者:User

標籤:

查看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] 手機衛士輸入框抖動和手機震動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.