详解Android动画之Tween Animation

前面讲了动画中的Frame动画,今天就来详细讲解一下Tween动画的使用。

同样,在开始实例演示之前,先引用官方文档中的一段话:

Tween动画是操作某个控件让其展现出旋转、渐变、移动、缩放的这么一种转换过程,我们成为补间动画。我们可以以XML形式定义动画,也可以编码实现。

如果以XML形式定义一个动画,我们按照动画的定义语法完成XML,并放置于/res/anim目录下,文件名可以作为资源ID被引用;如果由编码实现,我们需要使用到Animation对象。

如果用定义XML方式实现动画,我们需要熟悉一下动画XML语法:

ImageView image = (ImageView) findViewById(R.id.image);  

Animation testAnim = AnimationUtils.loadAnimation(this, R.anim.test);  

image.startAnimation(testAnim);  
ImageView image = (ImageView) findViewById(R.id.image);
Animation testAnim = AnimationUtils.loadAnimation(this, R.anim.test);
image.startAnimation(testAnim);

下面重点介绍一下插值器的概念:

首先要了解为什么需要插值器,因为在补间动画中,我们一般只定义关键帧(首帧或尾帧),然后由系统自动生成中间帧,生成中间帧的这个过程可以成为“插值”。插值器定义了动画变化的速率,提供不同的函数定义变化值相对于时间的变化规则,可以定义各种各样的非线性变化函数,比如加速、减速等。下面是几种常见的插值器:

AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator先加速再减速
AccelerateInterpolator@android:anim/accelerate_interpolator加速
AnticipateInterpolator@android:anim/anticipate_interpolator先回退一小步然后加速前进
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator在上一个基础上超出终点一小步再回到终点
BounceInterpolator@android:anim/bounce_interpolator最后阶段弹球效果
CycleInterpolator@android:anim/cycle_interpolator周期运动
DecelerateInterpolator@android:anim/decelerate_interpolator减速
LinearInterpolator@android:anim/linear_interpolator匀速
OvershootInterpolator@android:anim/overshoot_interpolator快速到达终点并超出一小步最后回到终点

然后我们可以这样使用一个插值器:

public float getInterpolation(float input) {  

    return input;  

}  
public float getInterpolation(float input) {
        return input;
    }
加速减速插值器定义如下:
public float getInterpolation(float input) {  

    return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;  

}  
public float getInterpolation(float input) {
        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
    }
有兴趣的话,大家可以尝试一下自定义一个插值器。

讲了这么久的概念,下面我们就结合实例来演示一下几种Tween动画的应用。

先来介绍一下旋转动画的使用,布局文件/res/layout/rotate.xml如下:

package com.scott.anim;  
  

import android.app.Activity;  


import android.os.Bundle;  


import android.view.View;  


import android.view.animation.Animation;  


import android.view.animation.AnimationUtils;  


import android.view.animation.LinearInterpolator;  


import android.view.animation.RotateAnimation;  

  

public class RotateActivity extends Activity {  

  

    private int currAngle;  


    private View piechart;  

  

    @Override  


    public void onCreate(Bundle savedInstanceState) {  


        super.onCreate(savedInstanceState);  

        setContentView(R.layout.rotate);  
  
        piechart = findViewById(R.id.piechart);  

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);  

        piechart.startAnimation(animation);  
    }  
      

    public void positive(View v) {  


        Animation anim = new RotateAnimation(currAngle, currAngle + 180, Animation.RELATIVE_TO_SELF, 0.5f,  


                Animation.RELATIVE_TO_SELF, 0.5f);  


        /** 匀速插值器 */  


        LinearInterpolator lir = new LinearInterpolator();  

        anim.setInterpolator(lir);  

        anim.setDuration(1000);  


        /** 动画完成后不恢复原状 */  


        anim.setFillAfter(true);  


        currAngle += 180;  


        if (currAngle > 360) {  


            currAngle = currAngle - 360;  

        }  
        piechart.startAnimation(anim);  
    }  
      

    public void negative(View v) {  


        Animation anim = new RotateAnimation(currAngle, currAngle - 180, Animation.RELATIVE_TO_SELF, 0.5f,  


                Animation.RELATIVE_TO_SELF, 0.5f);  


        /** 匀速插值器 */  


        LinearInterpolator lir = new LinearInterpolator();  

        anim.setInterpolator(lir);  

        anim.setDuration(1000);  


        /** 动画完成后不恢复原状 */  


        anim.setFillAfter(true);  


        currAngle -= 180;  


        if (currAngle < -360) {  


            currAngle = currAngle + 360;  

        }  
        piechart.startAnimation(anim);  
    }  
}  
package com.scott.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

public class RotateActivity extends Activity {

	private int currAngle;
	private View piechart;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rotate);

		piechart = findViewById(R.id.piechart);
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
		piechart.startAnimation(animation);
	}
	
	public void positive(View v) {
		Animation anim = new RotateAnimation(currAngle, currAngle + 180, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		/** 匀速插值器 */
		LinearInterpolator lir = new LinearInterpolator();
		anim.setInterpolator(lir);
		anim.setDuration(1000);
		/** 动画完成后不恢复原状 */
		anim.setFillAfter(true);
		currAngle += 180;
		if (currAngle > 360) {
			currAngle = currAngle - 360;
		}
		piechart.startAnimation(anim);
	}
	
	public void negative(View v) {
		Animation anim = new RotateAnimation(currAngle, currAngle - 180, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		/** 匀速插值器 */
		LinearInterpolator lir = new LinearInterpolator();
		anim.setInterpolator(lir);
		anim.setDuration(1000);
		/** 动画完成后不恢复原状 */
		anim.setFillAfter(true);
		currAngle -= 180;
		if (currAngle < -360) {
			currAngle = currAngle + 360;
		}
		piechart.startAnimation(anim);
	}
}
然后,看一下渐变动画,布局文件/res/layout/alpha.xml如下:
package com.scott.anim;  
  

import android.app.Activity;  


import android.os.Bundle;  


import android.util.Log;  


import android.view.View;  


import android.view.animation.AlphaAnimation;  


import android.view.animation.Animation;  


import android.view.animation.Animation.AnimationListener;  


import android.view.animation.AnimationUtils;  


import android.widget.ImageView;  

  

public class AlphaActivity extends Activity implements AnimationListener {  

      

    private ImageView splash;  

      

    @Override  


    protected void onCreate(Bundle savedInstanceState) {  


        super.onCreate(savedInstanceState);  

        setContentView(R.layout.alpha);  
          
        splash = (ImageView) findViewById(R.id.splash);  

        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);  


        anim.setAnimationListener(this);  

        splash.startAnimation(anim);  
    }  
      

    public void alpha(View view) {  


        Animation anim = new AlphaAnimation(1.0f, 0.0f);  


        anim.setDuration(3000);  


        anim.setFillAfter(true);  

        splash.startAnimation(anim);  
    }  
  

    @Override  


    public void onAnimationStart(Animation animation) {  


        Log.i("alpha", "onAnimationStart called.");  

    }  
  

    @Override  


    public void onAnimationEnd(Animation animation) {  


        Log.i("alpha", "onAnimationEnd called");  

    }  
  

    @Override  


    public void onAnimationRepeat(Animation animation) {  


        Log.i("alpha", "onAnimationRepeat called");  

    }  
}  
package com.scott.anim;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AlphaActivity extends Activity implements AnimationListener {
	
	private ImageView splash;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alpha);
		
		splash = (ImageView) findViewById(R.id.splash);
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
		anim.setAnimationListener(this);
		splash.startAnimation(anim);
	}
	
	public void alpha(View view) {
		Animation anim = new AlphaAnimation(1.0f, 0.0f);
		anim.setDuration(3000);
		anim.setFillAfter(true);
		splash.startAnimation(anim);
	}

	@Override
	public void onAnimationStart(Animation animation) {
		Log.i("alpha", "onAnimationStart called.");
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		Log.i("alpha", "onAnimationEnd called");
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		Log.i("alpha", "onAnimationRepeat called");
	}
}
接着看一下位移动画,布局文件/res/layout/translate.xml如下:
package com.scott.anim;  
  

import android.app.Activity;  


import android.os.Bundle;  


import android.view.View;  


import android.view.animation.Animation;  


import android.view.animation.AnimationUtils;  


import android.view.animation.OvershootInterpolator;  


import android.view.animation.TranslateAnimation;  


import android.widget.ImageView;  

  

public class TranslateActivity extends Activity {  

      

    private ImageView trans_iamge;  

      

    @Override  


    protected void onCreate(Bundle savedInstanceState) {  


        super.onCreate(savedInstanceState);  

        setContentView(R.layout.tanslate);  
        trans_iamge = (ImageView) findViewById(R.id.trans_image);  

        Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate);  


        anim.setFillAfter(true);  

        trans_iamge.startAnimation(anim);  
    }  
      

    public void translate(View view) {  


        Animation anim = new TranslateAnimation(200, 0, 300, 0);  


        anim.setDuration(2000);  


        anim.setFillAfter(true);  


        OvershootInterpolator overshoot = new OvershootInterpolator();  

        anim.setInterpolator(overshoot);  
        trans_iamge.startAnimation(anim);  
    }  
}  
package com.scott.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class TranslateActivity extends Activity {
	
	private ImageView trans_iamge;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tanslate);
		trans_iamge = (ImageView) findViewById(R.id.trans_image);
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate);
		anim.setFillAfter(true);
		trans_iamge.startAnimation(anim);
	}
	
	public void translate(View view) {
		Animation anim = new TranslateAnimation(200, 0, 300, 0);
		anim.setDuration(2000);
		anim.setFillAfter(true);
		OvershootInterpolator overshoot = new OvershootInterpolator();
		anim.setInterpolator(overshoot);
		trans_iamge.startAnimation(anim);
	}
}
最后,我们再来看以下缩放动画,布局文件/res/layout/scale.xml如下:
package com.scott.anim;  
  

import android.app.Activity;  


import android.os.Bundle;  


import android.view.View;  


import android.view.animation.Animation;  


import android.view.animation.AnimationUtils;  


import android.view.animation.BounceInterpolator;  


import android.view.animation.ScaleAnimation;  


import android.widget.ImageView;  

  

public class ScaleActivity extends Activity {  

      

    private ImageView scale_iamge;  

      

    @Override  


    protected void onCreate(Bundle savedInstanceState) {  


        super.onCreate(savedInstanceState);  

        setContentView(R.layout.scale);  
        scale_iamge = (ImageView) findViewById(R.id.scale_image);  

        Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);  


        anim.setFillAfter(true);  

        scale_iamge.startAnimation(anim);  
    }  
      

    public void sclae(View view) {  


        Animation anim = new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f,   


                Animation.RELATIVE_TO_SELF, 0.5f,   


                Animation.RELATIVE_TO_SELF, 0.5f);  


        anim.setDuration(2000);  


        anim.setFillAfter(true);  


        BounceInterpolator bounce = new BounceInterpolator();  

        anim.setInterpolator(bounce);  
        scale_iamge.startAnimation(anim);  
    }  
}  
package com.scott.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class ScaleActivity extends Activity {
	
	private ImageView scale_iamge;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scale);
		scale_iamge = (ImageView) findViewById(R.id.scale_image);
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);
		anim.setFillAfter(true);
		scale_iamge.startAnimation(anim);
	}
	
	public void sclae(View view) {
		Animation anim = new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f, 
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f);
		anim.setDuration(2000);
		anim.setFillAfter(true);
		BounceInterpolator bounce = new BounceInterpolator();
		anim.setInterpolator(bounce);
		scale_iamge.startAnimation(anim);
	}
}
几种动画运行效果如下图所示:

详解Android动画之Tween Animation详解Android动画之Tween Animation

详解Android动画之Tween Animation详解Android动画之Tween Animation

相关推荐