[android] 手机卫士输入框抖动和手机震动

来源:互联网 发布:网络批发干货平台 编辑:程序博客网 时间:2024/04/26 21:46

查看apiDemos,找到View/Animation/shake找到对应的动画代码,直接拷贝过来

当导入一个项目的时候,报R文件不存在,很多情况是xml文件出错了

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);

et_phone.startAnimation(shake);

动画的xml文件shake.xml

android:interpolator="@anim/cycle_7"

interpolator是插入器,可以定义动画的速度等

调用Animation对象的setInterpolator()方法,设置插入器,参数:Interpolator对象

匿名实现Interpolator接口,重写getInterpolation()方法,设置中自定义动画速率,传入一个flaot x

 

输入框的震动效果

获取Vibrator对象,调用getSystemService()方法,参数:VIBRATOR_SERVICE

调用Vibrator对象的vibrate()方法,参数:毫秒

需要添加权限android.permission.VIBRATE

 

这个可以做一些振动器~

/**     * 查询归属地     */    public void queryNumber(View v) {        phone = et_phone.getText().toString().trim();        if (TextUtils.isEmpty(phone)) {            //抖动动画            Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);            et_phone.startAnimation(shake);            //手机震动            vibrator.vibrate(2000);            Toast.makeText(this, "请输入手机号码", 0).show();            return;        }        String result = NumberQueryAddressUtil.queryAddress(phone);        tv_address.setText(result);    }

shake.xml

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

cycle_7.xml

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

转自:http://www.cnblogs.com/taoshihan/p/5425895.html




原创粉丝点击