1、视图
1 <LinearLayout 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 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:orientation="horizontal" 12 > 13 <Button 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:onClick="click1" 17 android:text="透明" 18 /> 19 <Button 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:onClick="click2" 23 android:text="缩放" 24 /> 25 26 <Button 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:onClick="click3" 30 android:text="旋转" 31 /> 32 33 <Button 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:onClick="click4" 37 android:text="平移" 38 /> 39 40 <Button 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:onClick="click5" 44 android:text="组合" 45 /> 46 </LinearLayout> 47 <LinearLayout 48 android:layout_width="match_parent" 49 android:layout_height="match_parent" 50 android:gravity="center" 51 > 52 <ImageView 53 android:id="@+id/iv" 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:src="@drawable/ic_launcher" 57 /> 58 </LinearLayout> 59 60 </LinearLayout>
2、android代码
1 package com.example.tweenanim; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.view.animation.AlphaAnimation; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationSet; 10 import android.view.animation.RotateAnimation; 11 import android.view.animation.ScaleAnimation; 12 import android.view.animation.TranslateAnimation; 13 import android.widget.ImageView; 14 15 public class MainActivity extends Activity { 16 private ImageView iv; 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 iv = (ImageView) findViewById(R.id.iv); 22 } 23 24 //透明度 25 public void click1(View view){ 26 //第一个参数表示开始透明度;0表示完全透明 27 //第二个参数表示结束透明度;1表示完全不透明 28 AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f); 29 animation.setDuration(2000);//设置动画时间为2妙 30 animation.setRepeatCount(2);//设置重复次数,因此总共3次 31 animation.setRepeatMode(Animation.REVERSE);//设置重复模式 32 iv.startAnimation(animation); 33 } 34 35 //缩放图片 36 public void click2(View view){ 37 //第一个参数为开始x轴缩放坐标;第二个参数为结束时x轴缩放坐标 38 //第三个参数为开始y轴缩放坐标;第四个参数为结束时y轴缩放坐标 39 //第五个参数为中心x坐标以什么为参照,Animation.RELATIVE_TO_SELF说明以自己为参照;第六个参数为参照坐标比例;第七、八与第五、六相同,只不过它指的是y轴 40 ScaleAnimation animation = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 41 animation.setDuration(2000); 42 animation.setRepeatCount(2); 43 animation.setRepeatMode(Animation.REVERSE); 44 iv.startAnimation(animation); 45 } 46 47 //旋转 48 public void click3(View view){ 49 //第一参数,开始旋转角度 50 //第二个参数为旋转到多少度 51 //第3-6个参数与缩放的第5-8的参数相同 52 RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 53 animation.setDuration(2000); 54 animation.setRepeatCount(2); 55 animation.setRepeatMode(Animation.REVERSE); 56 iv.startAnimation(animation); 57 } 58 59 //平移 60 public void click4(View view){ 61 //x轴 62 //第一个参数为参照类型:Animation.RELATIVE_TO_PARENT以父窗口为参照 63 //第二个参数为开始坐标比例 64 //第三个同第一个;第四个参数是说明要平移多长的比例;1.0说明要有父窗口的宽度 65 66 //y轴同上 67 68 69 TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); 70 animation.setDuration(2000); 71 animation.setRepeatCount(2); 72 animation.setRepeatMode(Animation.REVERSE); 73 iv.startAnimation(animation); 74 } 75 76 //组合动画 77 public void click5(View view){ 78 AnimationSet set = new AnimationSet(false); 79 80 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); 81 ta.setDuration(2000); 82 ta.setRepeatCount(2); 83 ta.setRepeatMode(Animation.REVERSE); 84 85 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 86 ra.setDuration(2000); 87 ra.setRepeatCount(2); 88 ra.setRepeatMode(Animation.REVERSE); 89 90 ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 91 sa.setDuration(2000); 92 sa.setRepeatCount(2); 93 sa.setRepeatMode(Animation.REVERSE); 94 95 set.addAnimation(ta); 96 set.addAnimation(ra); 97 set.addAnimation(sa); 98 iv.startAnimation(set); 99 } 100 101 }
原文:http://www.cnblogs.com/zhongyinghe/p/5364746.html