在res中建立文件夹anim,分别写下cycles.xml,shake1.xml,shake2.xml
cycles.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- android:cycles代表移动的速度 -->
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="1" />
shake1.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--水平移动 -->
<!--android:duration代表运行时间 -->
<!-- android:fromXDelta代表起始横坐标位置 (0,0)-->
<!-- android:toXDelta代表离横坐标起始位置的X距离为100 (100,0)-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromXDelta="0.0"
android:toXDelta="100.0"
android:interpolator="@anim/cycles"/>
shake2.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--垂直移动 -->
<!--android:duration代表运行时间 -->
<!-- android:fromXDelta代表起始纵坐标位置 (0,0)-->
<!-- android:toXDelta代表离横坐标起始位置的Y距离为100(0,100)-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromYDelta="0.0"
android:toYDelta="100.0"
android:interpolator="@anim/cycles"/>
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 tools:context="com.zzw.testshake.MainActivity" >
6
7 <ImageView
8 android:id="@+id/image2"
9 android:layout_width="100dp"
10 android:layout_height="100dp"
11 android:layout_alignParentBottom="true"
12 android:layout_centerHorizontal="true"
13 android:layout_marginBottom="78dp"
14 android:src="@drawable/b" />
15
16 <ImageView
17 android:id="@+id/image1"
18 android:layout_width="100dp"
19 android:layout_height="100dp"
20 android:layout_alignLeft="@+id/image2"
21 android:layout_alignParentTop="true"
22 android:layout_marginTop="87dp"
23 android:src="@drawable/a" />
24
25 <Button
26 android:id="@+id/button2"
27 style="?android:attr/buttonStyleSmall"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:layout_above="@+id/image2"
31 android:layout_toLeftOf="@+id/image2"
32 android:text="Button" />
33
34
35 <Button
36 android:id="@+id/button1"
37 style="?android:attr/buttonStyleSmall"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:layout_below="@+id/editText1"
41 android:layout_toLeftOf="@+id/image1"
42 android:text="Button" />
43
44 </RelativeLayout>
activity_main.xml
MainActivity:
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)); } }