在res中建立文件夹anim,分别写下cycles.xml,shake1.xml,shake2.xml
cycles.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- android:cycles代表移动的速度 --> 3 <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" 4 android:cycles="1" />
shake1.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <!--水平移动 --> 3 <!--android:duration代表运行时间 --> 4 <!-- android:fromXDelta代表起始横坐标位置 (0,0)--> 5 <!-- android:toXDelta代表离横坐标起始位置的X距离为100 (100,0)--> 6 <translate xmlns:android="http://schemas.android.com/apk/res/android" 7 android:duration="3000" 8 android:fromXDelta="0.0" 9 android:toXDelta="100.0" 10 android:interpolator="@anim/cycles"/>
shake2.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <!--垂直移动 --> 3 <!--android:duration代表运行时间 --> 4 <!-- android:fromXDelta代表起始纵坐标位置 (0,0)--> 5 <!-- android:toXDelta代表离横坐标起始位置的Y距离为100(0,100)--> 6 <translate xmlns:android="http://schemas.android.com/apk/res/android" 7 android:duration="3000" 8 android:fromYDelta="0.0" 9 android:toYDelta="100.0" 10 android:interpolator="@anim/cycles"/>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zzw.testshake.MainActivity" >
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="78dp"
android:src="@drawable/b" />
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/image2"
android:layout_alignParentTop="true"
android:layout_marginTop="87dp"
android:src="@drawable/a" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/image2"
android:layout_toLeftOf="@+id/image2"
android:text="Button" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_toLeftOf="@+id/image1"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<pre name="code" class="html">activity_main.xml
package com.zzw.testshake;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake1));
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake2));
Button bt1 = (Button) findViewById(R.id.button1);
bt1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake1));
Button bt2 = (Button) findViewById(R.id.button2);
bt2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake2));
}
}