本例為一密碼輸入框添加“Shake” 動畫效果,類比密碼輸入錯誤後左右擺動文字框以提示使用者密碼不正確。
代碼如下:
[html]
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.pw).startAnimation(shake);
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.pw).startAnimation(shake);
Shake效果是通過 translate 平移Animation 設定 cycleInterpolator 周期迴圈運動來實現的。
anim 的shake.xml
[html]
<translate
xmlns:android=”http://schemas.android.com/apk/res/android”
android:fromXDelta=”0″ android:toXDelta=”10″
android:duration=”1000″ android:interpolator=”@anim/cycle_7″ />
<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 定義如下:
[html] view plaincopyprint?
<cycleInterpolator
xmlns:android=”http://schemas.android.com/apk/res/android”
android:cycles=”7″ />
<cycleInterpolator
xmlns:android=”http://schemas.android.com/apk/res/android”
android:cycles=”7″ />文字框左右擺動7此產生擺動效果。
作者:mapdigit