首先在res下创建一个anim文件目录
存放有cycleinterpolator.xml和shake.xml
XML代码:cycleinterpolator.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="20" />
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="30" android:duration="700"
android:interpolator="@anim/cycleinterpolator" />
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/view"
>
<EditText
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="抖动"
/>
</LinearLayout>
java代码:
package com.mmax.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation animation = AnimationUtils.loadAnimation(this, R.anim.shake);
final EditText editText = (EditText) findViewById(R.id.textView);
final ViewGroup view = (ViewGroup)findViewById(R.id.view);
editText.startAnimation(animation);
Button but = (Button)findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.startAnimation(animation);
}
});
}
}