Android 實現(xiàn)為點擊事件添加震動效果
Android 點擊Button 實現(xiàn)震動效果教程
Overview
在Android 的點擊效果中,遇到震動效果的還是很多的。
接下來就讓我們看一下如何實現(xiàn)震動效果。
所需要的權(quán)限
如果我們在開發(fā)中需要使用到我們的震動,那么我們就需要申請一下權(quán)限:
<uses-permission android:name='android.permission.VIBRATE'/>
這樣我們的權(quán)限就申請好了。
我們震動效果的幫助類
創(chuàng)建一個名為VibrateHelp的點擊震動的幫助類。
然后看一下如何使用他的把:
public class VibrateHelp { private static Vibrator vibrator; /** * @ClassName:VibrateHelp - 簡單的震動 * @author:CaoJiaHao * @Param:context 調(diào)用震動類的 context * @param:millisecond 震動的時間 */ @SuppressWarnings('static-access') public static void vSimple(Context context, int millisecode) { vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(millisecode); } /** * @param : pattern 震動的形式 * @param : repeate 震動循環(huán)的次數(shù) * @ClassName:VibrateHelp - 復(fù)雜的震動 * @author:CaoJiaHao * @Param: context 調(diào)用復(fù)雜震動的context **/ @SuppressWarnings('static-access') public static void vComplicated(Context context, long[] pattern, int repeate) { vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(pattern, repeate); } /** *@ClassName:VibrateHelp - 停止震動 *@author:CaoJiaHao **/ public static void stop() { if (vibrator != null) vibrator.cancel(); }}
這樣的話我們的 震動幫助類就完成呢。
然后我們根據(jù)我們的源碼來分析一下:
我們需要將Vibrator 實例化一下。
然后我們創(chuàng)建我么你的簡單的震動模式。
接著創(chuàng)建我們比較復(fù)雜的震動模式。
這樣我們的點擊震動幫助類就完成了。
但是我們光有了幫助類是遠(yuǎn)遠(yuǎn)不夠的。我們還需要調(diào)用他才可以,不然我們的Helper Class 沒有任何作用。
封裝我們的震動點擊事件
首先,我們創(chuàng)建一個類,讓他控制我們的點擊震動效果。
我們創(chuàng)建一個名為ViewClickVibrate。然后先看一下源代碼:
public class ViewClickVibrate implements View.OnClickListener { private final int VIBRATE_TIME = 60; @Override public void onClick(View v) { VibrateHelp.vSimple(v.getContext(), VIBRATE_TIME); }}
這個就是我們的源代碼,但是需要注意的是,我們封裝的這個類,需要去調(diào)用我們的View.OnClickListener的接口.
這樣我們的點擊效果算是全部完成了。
最后我們看一下如何實現(xiàn)他吧。
ImageCategory.setOnClickListener(new ViewClickVibrate() { public void onClick(View v) { super.onClick(v); Global.Go(FinanceActivity.this, CategoryActivity.class); }});
這樣的一個點擊效果就完成了。
補充知識:android控件實現(xiàn)抖動的效果
這個程序的功能有可能在實際的開發(fā)中會用到,比如說Button左右晃動,或者上下的晃動效果,下面就給出代碼。
首先要定義一個xml文件,命名為shake
<?xml version='1.0' encoding='utf-8'?><translate xmlns:android='http://schemas.android.com/apk/res/android' android:fromXDelta='0' android:toXDelta='100' android:duration='1000' android:interpolator='@anim/cycle_7' />
接下來再定義一個xml文件,命名為cycle_7
<?xml version='1.0' encoding='utf-8'?><cycleInterpolator xmlns:android='http://schemas.android.com/apk/res/android' android:cycles='2' />
這兩個xml文件都要建在,res文件夾下面的anim文件中,如果沒有anim文件,可以自己建一個。
然后就是新建一個activity代碼如下
import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void go(View v){ Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);//加載動畫資源文件 findViewById(R.id.tv).startAnimation(shake); //給組件播放動畫效果 } }
下面給出main.xml
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='fill_parent' android:layout_height='fill_parent' android:orientation='vertical' android:gravity='center_horizontal|center_vertical' > <EditText android:layout_width='fill_parent' android:layout_height='wrap_content' android: android:text='wojiuahiswo' /> <Button android:layout_width='fill_parent' android:layout_height='wrap_content' android:text='go' android:onClick='go' /> </LinearLayout>
這樣就實現(xiàn)了一個edittext控件的抖動效果,這里說明一下cycle_7.xml文件中android:cycles='2' 這一項是設(shè)置抖動的次數(shù)的,2為抖動兩次。而shake.xml中
android:fromXDelta='0' android:toXDelta='100'
是控制抖動的范圍的,上面的代碼是在x軸進(jìn)行抖動,如果把x替換為y就是在y軸進(jìn)行抖動,當(dāng)然也可以在x,y軸同時抖動。
以上這篇Android 實現(xiàn)為點擊事件添加震動效果就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python獲取抖音關(guān)注列表封號賬號的實現(xiàn)代碼2. ajax請求添加自定義header參數(shù)代碼3. Python數(shù)據(jù)分析之pandas函數(shù)詳解4. 解決Python 進(jìn)程池Pool中一些坑5. php測試程序運行速度和頁面執(zhí)行速度的代碼6. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. 三個不常見的 HTML5 實用新特性簡介8. 使用.net core 自帶DI框架實現(xiàn)延遲加載功能9. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究10. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析
