Android實(shí)現(xiàn)手機(jī)震動(dòng)抖動(dòng)效果的方法
Android手機(jī)震動(dòng)抖動(dòng)效果的實(shí)現(xiàn)
(1)布局文件如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="16dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_text" android:layout_below="@+id/et_text" android:layout_marginTop="38dp" android:text="提交" /> </RelativeLayout>
(2)MainActivity.java
package com.example.test11; import android.app.Activity; import android.os.Bundle; import android.os.Vibrator; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_text; private Button btn_submit; /** * 手機(jī)振動(dòng)器 */ private Vibrator vibrator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_text = (EditText) this.findViewById(R.id.et_text); btn_submit = (Button) this.findViewById(R.id.btn_submit); // 震動(dòng)效果的系統(tǒng)服務(wù) vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); btn_submit.setOnClickListener(new OnClickListener() { String text = et_text.getText().toString().trim(); public void onClick(View v) { if (TextUtils.isEmpty(text)) { Toast.makeText(MainActivity.this, "內(nèi)容為空", 0).show(); Animation shake = AnimationUtils.loadAnimation( MainActivity.this, R.anim.shake); et_text.startAnimation(shake); /* * 震動(dòng)的方式 */ // vibrator.vibrate(2000);//振動(dòng)兩秒 // 下邊是可以使震動(dòng)有規(guī)律的震動(dòng) -1:表示不重復(fù) 0:循環(huán)的震動(dòng) long[] pattern = { 200, 2000, 2000, 200, 200, 200 }; vibrator.vibrate(pattern, -1); } } }); } }
(3)使用到的兩個(gè)動(dòng)畫文件如下:
cycle_7.xml
<?xml version="1.0" encoding="utf-8"?> <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />
shake.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXDelta="0" android:interpolator="@anim/cycle_7" android:toXDelta="10" />
(4)需要使用的權(quán)限:
<uses-permission android:name="android.permission.VIBRATE" />
這個(gè)效果一般只有在真機(jī)上可以做到,不在上圖展示。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
桌面快捷方式的出現(xiàn)方便了用戶操作,在某些程度上提高了用戶體驗(yàn),接下來(lái)將介紹下Android創(chuàng)建/驗(yàn)證/刪除桌面快捷方式的實(shí)現(xiàn)思路及代碼,感興趣的朋友可以了解下,或許本文可以幫助到你2013-02-02JetpackCompose Navigation導(dǎo)航實(shí)現(xiàn)流程
Navigation是Jetpack用于Android導(dǎo)航的組件,作用是處理頁(yè)面跳轉(zhuǎn),以及頁(yè)面跳轉(zhuǎn)過(guò)程中的交互。使用Navigation,你就需要為每個(gè)頁(yè)面設(shè)定一條唯一路徑,它是一個(gè)String常量,形式是DeepLink的樣子,從一個(gè)頁(yè)面跳轉(zhuǎn)到另一個(gè)頁(yè)面,它通過(guò)輸入目的地的路徑進(jìn)行轉(zhuǎn)跳2023-01-01Eclipse打開時(shí)“發(fā)現(xiàn)了以元素''d:skin''”開頭的無(wú)效內(nèi)容。此處不應(yīng)含有子元素的解決方法
這篇文章主要介紹了Eclipse打開時(shí)“發(fā)現(xiàn)了以元素'd:skin'”開頭的無(wú)效內(nèi)容。此處不應(yīng)含有子元素的解決方法,涉及Android sdk中devices.xml文件的修改,需要的朋友可以參考下2016-01-01Android RecyclerView設(shè)置下拉刷新的實(shí)現(xiàn)方法
這篇文章主要介紹了Android RecyclerView設(shè)置下拉刷新的實(shí)現(xiàn)方法,希望通過(guò)本文通過(guò)SwipeRefreshLayout方式實(shí)現(xiàn)下拉刷新,需要的朋友可以參考下2017-10-10Android中Java instanceof關(guān)鍵字全面解析
instanceof關(guān)鍵字用于判斷一個(gè)引用類型變量所指向的對(duì)象是否是一個(gè)類(或接口、抽象類、父類)的實(shí)例.這篇文章主要介紹了Android中Java instanceof關(guān)鍵字全面解析的相關(guān)資料,需要的朋友可以參考下2016-07-07Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Android自定義view利用Xfermode實(shí)現(xiàn)動(dòng)態(tài)文字加載動(dòng)畫
這篇文章主要介紹了Android自定義view利用Xfermode實(shí)現(xiàn)動(dòng)態(tài)文字加載動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07