本文共 5128 字,大约阅读时间需要 17 分钟。
3.0以前,android支持两种动画模式,Tween Animation,Frame Animation,在android3.0中又引入了一个新的动画系统:Property Animation,这三种动画模式在SDK中被称为Property Animation,View Animation,Drawable Animation。
我今天要说的就是Tween Animation.要实现它有两种方式。大致的步骤是这样的:
下面是一个简单的代码:
package com.summer.animationutils;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity { private Button button_rotate,button_alpha,button_translate,button_scale; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=(ImageView) findViewById(R.id.imageview1); button_rotate=(Button) findViewById(R.id.button_rotate); button_alpha=(Button) findViewById(R.id.button_alpha); button_translate=(Button) findViewById(R.id.button_translate); button_scale=(Button) findViewById(R.id.button_scale); button_rotate.setOnClickListener(new RotateAnimationListener()); button_alpha.setOnClickListener(new AlphaAnimationListener()); button_translate.setOnClickListener(new TranslateAnimationListener()); button_scale.setOnClickListener(new ScaleAnimationListener()); } class RotateAnimationListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet=new AnimationSet(true); RotateAnimation rotateAnimation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(1500); animationSet.addAnimation(rotateAnimation); imageView.startAnimation(animationSet); } } class AlphaAnimationListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet=new AnimationSet(true); AlphaAnimation alphaAnimation=new AlphaAnimation(1,0); alphaAnimation.setDuration(1500); animationSet.addAnimation(alphaAnimation); imageView.startAnimation(animationSet); } } class TranslateAnimationListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet=new AnimationSet(true); TranslateAnimation translateAnimation=new TranslateAnimation( Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,1.0f); translateAnimation.setDuration(1500); animationSet.addAnimation(translateAnimation); imageView.startAnimation(animationSet); } } class ScaleAnimationListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet=new AnimationSet(true); ScaleAnimation scaleAnimation=new ScaleAnimation(1,0.1f,1,0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(1500); animationSet.addAnimation(scaleAnimation); imageView.startAnimation(animationSet); } }}
小总结:
大致的步骤如下:
下面是我的一个动画文件示例分解:
如alpha.xml。简单明了清晰再看rotate.xml文件,其中pivotX属性的值分别代表着三种不同的情况(当然其他的属性值也是有这个情况滴)
再看translate.xml
最后是scale.xml.
完成了xml动画文件的设置,下一步当然是要进行使用了,否则要它做什么,我们可以使用下慢的代码来进行动画的使用。
Animation animation=(Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);imageView.startAnimation(animation);
同样的,其他的动画也可以这么做。
偷偷的告诉你,可以在一个xml文件中同时设置好几个子标签,来完成好几个动画的叠加效果哦。(即如果想要共享一个Interpolator的话,需要将android:shareInterpolator的值设置为true)。至于什么是Interpalotor,就是android自带的一些动画的效果。
转载地址:http://nuvul.baihongyu.com/