import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.EditText; public class AnimActivity extends Activity { EditText edit; Button btn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit = (EditText)findViewById(R.id.edit); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation anim = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.myanim); edit.startAnimation(anim); } }); } }
在res/anim下创建cyle.xml
<?xml version="1.0" encoding="utf-8"?> <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="2" />
在res/anim下创建myanim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@anim/cycle" > <translate android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-10" android:toYDelta="-10" /> <translate android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:startOffset="300" android:toXDelta="10" android:toYDelta="-10" /> <translate android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:startOffset="600" android:toXDelta="-10" android:toYDelta="10" /> <translate android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:startOffset="900" android:toXDelta="10" android:toYDelta="10" /> </set>
mian.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:layout_marginTop="100dp" android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击按钮、此窗口抖动" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮" /> </LinearLayout>
效果: