当前位置: 移动技术网 > 移动技术>移动开发>Android > 浅析Android手机卫士之抖动输入框和手机震动

浅析Android手机卫士之抖动输入框和手机震动

2019年07月24日  | 移动技术网移动技术  | 我要评论

查看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" />

以上所述是小编给大家介绍的android手机卫士之输入框抖动和手机震动的相关内容,希望对大家有所帮助!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网