Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動效果
Android 點(diǎn)擊Button 實(shí)現(xiàn)震動效果教程
Overview
在Android 的點(diǎn)擊效果中,遇到震動效果的還是很多的。
接下來就讓我們看一下如何實(shí)現(xiàn)震動效果。
所需要的權(quán)限
如果我們在開發(fā)中需要使用到我們的震動,那么我們就需要申請一下權(quán)限:
<uses-permission android:name='android.permission.VIBRATE'/>
這樣我們的權(quán)限就申請好了。
我們震動效果的幫助類
創(chuàng)建一個名為VibrateHelp的點(diǎn)擊震動的幫助類。
然后看一下如何使用他的把:
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 實(shí)例化一下。
然后我們創(chuàng)建我么你的簡單的震動模式。
接著創(chuàng)建我們比較復(fù)雜的震動模式。
這樣我們的點(diǎn)擊震動幫助類就完成了。
但是我們光有了幫助類是遠(yuǎn)遠(yuǎn)不夠的。我們還需要調(diào)用他才可以,不然我們的Helper Class 沒有任何作用。
封裝我們的震動點(diǎn)擊事件
首先,我們創(chuàng)建一個類,讓他控制我們的點(diǎn)擊震動效果。
我們創(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的接口.
這樣我們的點(diǎn)擊效果算是全部完成了。
最后我們看一下如何實(shí)現(xiàn)他吧。
ImageCategory.setOnClickListener(new ViewClickVibrate() { public void onClick(View v) { super.onClick(v); Global.Go(FinanceActivity.this, CategoryActivity.class); }});
這樣的一個點(diǎn)擊效果就完成了。
補(bǔ)充知識:android控件實(shí)現(xiàn)抖動的效果
這個程序的功能有可能在實(shí)際的開發(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>
這樣就實(shí)現(xiàn)了一個edittext控件的抖動效果,這里說明一下cycle_7.xml文件中android:cycles='2' 這一項(xiàng)是設(shè)置抖動的次數(shù)的,2為抖動兩次。而shake.xml中
android:fromXDelta='0' android:toXDelta='100'
是控制抖動的范圍的,上面的代碼是在x軸進(jìn)行抖動,如果把x替換為y就是在y軸進(jìn)行抖動,當(dāng)然也可以在x,y軸同時抖動。
以上這篇Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動效果就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法2. Python matplotlib 繪制雙Y軸曲線圖的示例代碼3. 利用ajax+php實(shí)現(xiàn)商品價格計算4. JSP頁面實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能5. asp知識整理筆記4(問答模式)6. java 優(yōu)雅關(guān)閉線程池的方案7. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)8. 詳解idea中web.xml默認(rèn)版本問題解決9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. jsp EL表達(dá)式詳解
