android 震动效果类

设置震动(Vibration)事件中,必须要知道命令其震动的时间长短、震动事件的周期等,而在Android里设置的数值,皆是以毫秒(1000毫秒=1秒)来做计算,所以在做设置时,必须要注意一下,如果设置的时间值太小的话,会感觉不出来。

要让手机乖乖的震动,需创建Vibrator对象,通过调用vibrate方法来达到震动的目的,在Vibrator的构造器中有4个参数,前3个的值是设置震动的大小,在这边可以把数值改成一大一小,这样就可以明显感觉出震动的差异,而最后一个值是设置震动的时间。

以下的范例设置每个震动的模式都不一样,当repeat = 0时,震动会一直持续,若repeat = −1时,震动只会出现一轮,运行完毕后就不会再有动作。

import android.content.Context;import android.os.Vibrator;/** * 震动 如果报错请在 androidManifest.xml中加入 以下语句 * * <uses-permission android:name="android.permission.VIBRATE" /> * @author alex.xin * */public class Vibrate { final static String TAG = "GameEngine"; Vibrator vibrator; long[] pattern = { 800, 50, 400, 30 }; // 震动周期 可以自己设置 public Vibrate(Context context) { vibrator = (Vibrator) context .getSystemService(context.VIBRATOR_SERVICE); } public void playVibrate(int type) { vibrator.vibrate(pattern, type); // -1不重复,非-1为从pattern的指定下标开始重复 } public void Stop() { vibrator.cancel(); }}

试着把震动的大小做多变化的改变,在long[]里面可以多做一些变化,上面的程序对参数的用法作了示范。

你可能感兴趣的:(android,String,service,Class,手机,import)