控件抖动配合手机震动

来源:互联网 发布:淘宝上卖什么利润最大 编辑:程序博客网 时间:2024/05/07 08:10

**应用场景:
表单提交不符合规范、密码输入错误等**

控件抖动:

1、在res下的anim文件夹下创建xml文件(以下为demo.xml),没有anim文件夹自己创建。

demo.xml内容: 
<translate xmlns:android="http://schemas.android.com/apk/res/android"     android:fromXDelta="0"     android:toXDelta="10"     android:duration="1000"     android:interpolator="@anim/cycle" />

2、然后再创建xml文件(以下为cycle.xml)

cycle.xml内容:
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />

3、控件使用:

//抖动起来Animation shake=AnimationUtils.loadAnimation(上下文, R.anim.shake);控件.startAnimation(shake);

xml里面的值可以自己去尝试修改,看看效果

===============================================

很多时候控件抖动会和手机震动配合使用,所以就赋上手机震动代码吧

先加权限:

<uses-permission android:name="android.permission.VIBRATE" />
public void demo(Context context){         //获得服务            Vibrator vibrator = (Vibrator)context. getSystemService(Context.VIBRATOR_SERVICE);            //震动两秒            vibrator.vibrate(2000);}

如果需要控制震动规律和次数可以改用下面的方法:

参数1:停1秒震动2秒再停1秒震动2秒参数2:重复参数1几次,不重复就传-1vibrator.vibrate(new long[]{1000,2000,1000,2000},1);
0 0
原创粉丝点击