Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果
Android 點(diǎn)擊Button 實(shí)現(xiàn)震動(dòng)效果教程
Overview
在Android 的點(diǎn)擊效果中,遇到震動(dòng)效果的還是很多的。
接下來就讓我們看一下如何實(shí)現(xiàn)震動(dòng)效果。
所需要的權(quán)限
如果我們?cè)陂_發(fā)中需要使用到我們的震動(dòng),那么我們就需要申請(qǐng)一下權(quán)限:
<uses-permission android:name='android.permission.VIBRATE'/>
這樣我們的權(quán)限就申請(qǐng)好了。
我們震動(dòng)效果的幫助類
創(chuàng)建一個(gè)名為VibrateHelp的點(diǎn)擊震動(dòng)的幫助類。
然后看一下如何使用他的把:
public class VibrateHelp { private static Vibrator vibrator; /** * @ClassName:VibrateHelp - 簡(jiǎn)單的震動(dòng) * @author:CaoJiaHao * @Param:context 調(diào)用震動(dòng)類的 context * @param:millisecond 震動(dòng)的時(shí)間 */ @SuppressWarnings('static-access') public static void vSimple(Context context, int millisecode) { vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(millisecode); } /** * @param : pattern 震動(dòng)的形式 * @param : repeate 震動(dòng)循環(huán)的次數(shù) * @ClassName:VibrateHelp - 復(fù)雜的震動(dòng) * @author:CaoJiaHao * @Param: context 調(diào)用復(fù)雜震動(dòng)的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 - 停止震動(dòng) *@author:CaoJiaHao **/ public static void stop() { if (vibrator != null) vibrator.cancel(); }}
這樣的話我們的 震動(dòng)幫助類就完成呢。
然后我們根據(jù)我們的源碼來分析一下:
我們需要將Vibrator 實(shí)例化一下。
然后我們創(chuàng)建我么你的簡(jiǎn)單的震動(dòng)模式。
接著創(chuàng)建我們比較復(fù)雜的震動(dòng)模式。
這樣我們的點(diǎn)擊震動(dòng)幫助類就完成了。
但是我們光有了幫助類是遠(yuǎn)遠(yuǎn)不夠的。我們還需要調(diào)用他才可以,不然我們的Helper Class 沒有任何作用。
封裝我們的震動(dòng)點(diǎn)擊事件
首先,我們創(chuàng)建一個(gè)類,讓他控制我們的點(diǎn)擊震動(dòng)效果。
我們創(chuàng)建一個(gè)名為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); }}
這個(gè)就是我們的源代碼,但是需要注意的是,我們封裝的這個(gè)類,需要去調(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); }});
這樣的一個(gè)點(diǎn)擊效果就完成了。
補(bǔ)充知識(shí):android控件實(shí)現(xiàn)抖動(dòng)的效果
這個(gè)程序的功能有可能在實(shí)際的開發(fā)中會(huì)用到,比如說Button左右晃動(dòng),或者上下的晃動(dòng)效果,下面就給出代碼。
首先要定義一個(gè)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' />
接下來再定義一個(gè)xml文件,命名為cycle_7
<?xml version='1.0' encoding='utf-8'?><cycleInterpolator xmlns:android='http://schemas.android.com/apk/res/android' android:cycles='2' />
這兩個(gè)xml文件都要建在,res文件夾下面的anim文件中,如果沒有anim文件,可以自己建一個(gè)。
然后就是新建一個(gè)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);//加載動(dòng)畫資源文件 findViewById(R.id.tv).startAnimation(shake); //給組件播放動(dòng)畫效果 } }
下面給出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)了一個(gè)edittext控件的抖動(dòng)效果,這里說明一下cycle_7.xml文件中android:cycles='2' 這一項(xiàng)是設(shè)置抖動(dòng)的次數(shù)的,2為抖動(dòng)兩次。而shake.xml中
android:fromXDelta='0' android:toXDelta='100'
是控制抖動(dòng)的范圍的,上面的代碼是在x軸進(jìn)行抖動(dòng),如果把x替換為y就是在y軸進(jìn)行抖動(dòng),當(dāng)然也可以在x,y軸同時(shí)抖動(dòng)。
以上這篇Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python中Anaconda3 安裝gdal庫的方法2. PHP AOP教程案例3. python用zip壓縮與解壓縮4. python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)5. Notepad++如何配置python?配置python操作流程詳解6. Python 簡(jiǎn)介7. Python操作Excel工作簿的示例代碼(*.xlsx)8. JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實(shí)例9. Python importlib模塊重載使用方法詳解10. Python自動(dòng)化之定位方法大殺器xpath
