标签:
在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:
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>
MainActivity:
1 package com.zzw.testshake; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.animation.AnimationUtils; 6 import android.widget.Button; 7 import android.widget.EditText; 8 import android.widget.ImageView; 9 10 public class MainActivity extends Activity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 17 ImageView image1 = (ImageView) findViewById(R.id.image1); 18 image1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake1)); 19 20 ImageView image2 = (ImageView) findViewById(R.id.image2); 21 image2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake2)); 22 23 Button bt1 = (Button) findViewById(R.id.button1); 24 bt1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake1)); 25 26 Button bt2 = (Button) findViewById(R.id.button2); 27 bt2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake2)); 28 29 } 30 31 }
标签:
原文地址:http://www.cnblogs.com/zzw1994/p/4979624.html