截图如下:
1. Activity 很简单,如下:
package com.example.viewanimationshake;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener {
private Button button;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.login);
editText = (EditText) findViewById(R.id.pw);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
editText.startAnimation(shake);
}
}
2.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="请输入您的密码:"
/>
<EditText android:id="@+id/pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:singleLine="true"
android:inputType="textPassword"
/>
<Button android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
/>
</LinearLayout>
3.动画的两个文件
<?xml version="1.0" encoding="utf-8"?>
<!-- interpolator 翻译为“内插程序”,
官方文档解释:An interpolator defines the rate of change of an animation.
This allows the basic animation effects (alpha, scale, translate, rotate)
to be accelerated, decelerated, repeated, etc.
通俗来讲就是android:interpolator 这个属性允许对动画做些改变,如加速,重复等。
"@anim/cycle_7" 是对自定义interpolator 的引用。-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="@anim/cycle_7"
android:toXDelta="10" />
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />
源代码在这里: 点击打开链接