Android ApiDemos示例解析(98):Views->Animation->Shake

来源:互联网 发布:java中的流是什么意思 编辑:程序博客网 时间:2024/05/16 19:21

本例为一密码输入框添加“Shake” 动画效果,模拟密码输入错误后左右摆动文本框以提示用户密码不正确。

代码如下:

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);findViewById(R.id.pw).startAnimation(shake);


 

Shake效果是通过 translate 平移Animation 设置 cycleInterpolator 周期循环运动来实现的。

anim 的shake.xml

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

而anim/cycle_7 定义如下:

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

文本框左右摆动7此产生摆动效果。