主AC包含各种API:
package com.example.tween;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv=(ImageView) findViewById(R.id.iv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /** * 透明度 * @param v */ public void click1(View v) { AlphaAnimation animation=new AlphaAnimation(0.0f,1.0f); //动画播放时间 animation.setDuration(2000); //重复几次 一个3次 animation.setRepeatCount(2); animation.setRepeatMode(AlphaAnimation.REVERSE); iv.startAnimation(animation); } /** * 缩放 * @param v */ public void click2(View v) { //参数含义很简单 开始x比例 最终x比例 开始Y比例……缩放中心的类型,缩放中心位置(比例) ScaleAnimation animation=new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //动画播放时间 animation.setDuration(2000); //重复几次 一个3次 animation.setRepeatCount(2); animation.setRepeatMode(Animation.REVERSE); iv.startAnimation(animation); } /** * 旋转 * @param v */ public void click3(View v) { RotateAnimation animation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //动画播放时间 animation.setDuration(2000); //重复几次 一个3次 animation.setRepeatCount(2); animation.setRepeatMode(Animation.REVERSE); iv.startAnimation(animation); } /** * 平移 * @param v */ public void click4(View v) { TranslateAnimation animation= new TranslateAnimation (Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f); //动画播放时间 animation.setDuration(2000); //重复几次 一个3次 animation.setRepeatCount(2); animation.setRepeatMode(Animation.REVERSE); iv.startAnimation(animation); } /** * 组合 * @param v */ public void click5(View v) { AnimationSet animationSet=new AnimationSet(false); RotateAnimation ra=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //动画播放时间 ra.setDuration(2000); //重复几次 一个3次 ra.setRepeatCount(2); ra.setRepeatMode(Animation.REVERSE); TranslateAnimation ta= new TranslateAnimation (Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.5f); //动画播放时间 ta.setDuration(2000); //重复几次 一个3次 ta.setRepeatCount(2); ta.setRepeatMode(Animation.REVERSE); ScaleAnimation sa=new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //动画播放时间 sa.setDuration(2000); //重复几次 一个3次 sa.setRepeatCount(2); sa.setRepeatMode(Animation.REVERSE); animationSet.addAnimation(ra); animationSet.addAnimation(ta); animationSet.addAnimation(sa); iv.startAnimation(animationSet); } /** * XMl文件指定动画 * @param v */ public void click6(View v) { iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_anim)); }}
也可使用XMl文件定义:其中有p的表示parent
很简单