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 - 復雜的震動 * @author:CaoJiaHao * @Param: context 調(diào)用復雜震動的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)建我們比較復雜的震動模式。
這樣我們的點擊震動幫助類就完成了。
但是我們光有了幫助類是遠遠不夠的。我們還需要調(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:id="@+id/tv" 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ù)的,2為抖動兩次。而shake.xml中
android:fromXDelta="0"
android:toXDelta="100"
是控制抖動的范圍的,上面的代碼是在x軸進行抖動,如果把x替換為y就是在y軸進行抖動,當然也可以在x,y軸同時抖動。
以上這篇Android 實現(xiàn)為點擊事件添加震動效果就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android NestedScrolling嵌套滾動的示例代碼
這篇文章主要介紹了Android NestedScrolling嵌套滾動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android編程實現(xiàn)啟動另外的APP及傳遞參數(shù)的方法
這篇文章主要介紹了Android編程實現(xiàn)啟動另外的APP及傳遞參數(shù)的方法,涉及Activity啟動及Intent設置相關(guān)操作技巧,需要的朋友可以參考下2017-05-05關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式
這篇文章主要介紹了關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03Android制作登錄頁面并且記住賬號密碼功能的實現(xiàn)代碼
這篇文章主要介紹了Android制作登錄頁面并且記住賬號密碼功能的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Android 進度條 ProgressBar的實現(xiàn)代碼(隱藏、出現(xiàn)、加載進度)
這篇文章主要介紹了Android 進度條 ProgressBar的實現(xiàn)代碼 (隱藏、出現(xiàn)、加載進度),代碼簡單易懂非常不錯,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04