Android-补间动画
一 动画的分类
1,View Animation即Tween Animation**补间动画**
用于对象本身的变化例如:
主要的类型
Alpha透明度:Alpha Animation渐变动画
scale缩放大小:ScaleAnimation旋转动画
Translate位置移动:Translate Animation位移动画
rotate旋转:Trranslate Animation旋转动画
2 Drawable Animation即Frame Animation帧动画
用于对象之间的变化
3 Property Animation 属性动画,可以不用,上面两种即可
View Animation和ObjectAnimation
二 使用方法
1,创建动画
在xml中定义(官方建议方式)
通过代码动态实现
2,为View指定动画
mImageView.startAnimation(_scaleAnimation);
3,监控动画的执行过程
_alphaAnimation.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationStart");
}
@Override
public void onAnimationRepeat(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationRepeat");
}
@Override
public void onAnimationEnd(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationEnd");
}
});
下面是一个综合补间动画Demo:
运行结果是点击四个不同的按钮,图片显示上面的四种不同的动画,
点击comples按钮,四种动画一起启动,分别用了xml方式和动态代码方式
主Activity类:
public class MainActivity extends Activity
{
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化图片对象
mImageView = (ImageView)findViewById(R.id.imageView1);
//定义透明按钮,并设置点击事件
Button _buttonAlpha = (Button)findViewById(R.id.buttonAlpha);
_buttonAlpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));
//定义旋转按钮,并设置点击事件
Button _buttonRotate = (Button)findViewById(R.id.buttonRotate);
_buttonRotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));
//定义缩放按钮,并设置点击事件
Button _buttonScale = (Button)findViewById(R.id.buttonScale);
_buttonScale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));
//定义位移按钮,并设置点击事件
Button _buttonTranslate = (Button)findViewById(R.id.buttonTranslate);
_buttonTranslate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));
//定义复杂按钮,并设置点击事件
Button _buttonComplex = (Button)findViewById(R.id.buttonComplex);
_buttonComplex.setOnClickListener(new AnimationClickListener(AnimationType.Complex));
}
//动画枚举类
enum AnimationType
{
Alpha,
Rotate,
Scale,
Translate,
Complex
}
//实现的动画的事件监听类
class AnimationClickListener implements OnClickListener
{
private AnimationType mAnimationType;
public AnimationClickListener(AnimationType p_AnimationType)
{
mAnimationType = p_AnimationType;
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
switch (mAnimationType)
{
case Alpha:
//1,----------------动态编码方式-----------------
/*//定义动画
AlphaAnimation _alphaAnimation = new AlphaAnimation(1f, 0.1f);
_alphaAnimation.setDuration(3000);
_alphaAnimation.setFillAfter(true);
_alphaAnimation.setRepeatCount(5);
_alphaAnimation.setRepeatMode(Animation.REVERSE);
_alphaAnimation.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationStart");
}
@Override
public void onAnimationRepeat(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationRepeat");
}
@Override
public void onAnimationEnd(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationEnd");
}
});
//启动动画
mImageView.startAnimation(_alphaAnimation); */
//2 --------------xml方式-------------------
Animation _animationUtils = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_alpha_anim);
mImageView.startAnimation(_animationUtils);
break;
case Rotate:
//1,--------------动态编码方式--------------
/*//定义动画
RotateAnimation _rotateAnimation = new RotateAnimation
(0, //起始度数
90, //旋转的度数
Animation.RELATIVE_TO_PARENT,//旋转中心的x坐标的参照物,是参照父布局,还是参照动画本身
0f, //x的值
Animation.RELATIVE_TO_PARENT, //旋转中心的y坐标的参照物,是参照父布局,还是参照动画本身
0f);//y的值
//RotateAnimation _rotateAnimation3 = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
//设置动画重复次数
_rotateAnimation.setRepeatCount(3);
//设置动画重复模式
_rotateAnimation.setRepeatMode(Animation.REVERSE);
//设置动画的完成状态
_rotateAnimation.setFillAfter(true);
//设置选装时间间隔
_rotateAnimation.setDuration(3000);
//设置动画的监听事件
_rotateAnimation.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationStart");
}
@Override
public void onAnimationRepeat(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationRepeat");
}
@Override
public void onAnimationEnd(Animation arg0)
{
// TODO Auto-generated method stub
Log.i("chengzhi", "onAnimationEnd");
}
});
//启动动画
mImageView.startAnimation(_rotateAnimation);
break;*/
Animation _rotateAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_rotate_anim);
mImageView.startAnimation(_rotateAnimation);
break;
case Scale:
//1,--------------动态编码方式--------------
/*//定义动画
ScaleAnimation _scaleAnimation = new ScaleAnimation(
0, 2.0f,
0, 2.0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
_scaleAnimation.setDuration(3000);
//?s
_scaleAnimation.setZAdjustment(10);
_scaleAnimation.setRepeatCount(3);
_scaleAnimation.setRepeatMode(Animation.REVERSE);
//启动动画
mImageView.startAnimation(_scaleAnimation);
*/
//2 -----------xml方式--------------
Animation _scaleAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_scale_anim);
mImageView.startAnimation(_scaleAnimation);
break;
case Translate:
//1,--------------动态编码方式--------------
/*TranslateAnimation _translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2f,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2f);
_translateAnimation.setDuration(3000);
_translateAnimation.setRepeatCount(3);
_translateAnimation.setRepeatMode(Animation.REVERSE);
mImageView.startAnimation(_translateAnimation);
*/
//2 -----------xml方式--------------
Animation _translateAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_translate_anim);
mImageView.startAnimation(_translateAnimation);
break;
case Complex:
//1,--------------动态编码方式--------------
/* AlphaAnimation _alphaAnimation2 = new AlphaAnimation(1.0f, 0.1f);
_alphaAnimation2.setRepeatCount(5);
//_alphaAnimation2.setDuration(3000);
ScaleAnimation _scaleAnimation2 = new ScaleAnimation
(0, 2.0f,
0, 2.0f,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
_scaleAnimation2.setRepeatCount(5);
//_scaleAnimation2.setDuration(3000);
RotateAnimation _rotateAnimation2 = new RotateAnimation
( 0, 360,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
_rotateAnimation2.setRepeatCount(5);
//_rotateAnimation2.setDuration(3000);
TranslateAnimation _translateAnimation2 = new TranslateAnimation
(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2f,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2f);
_translateAnimation2.setRepeatCount(5);
//_translateAnimation2.setDuration(3000);
//设置动画集对象
AnimationSet _animationSet = new AnimationSet(true);
_animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
_animationSet.addAnimation(_translateAnimation2);
_animationSet.addAnimation(_rotateAnimation2);
_animationSet.addAnimation(_scaleAnimation2);
_animationSet.addAnimation(_alphaAnimation2);
_animationSet.setDuration(10000);
mImageView.startAnimation(_animationSet);*/
//2 -----------xml方式--------------
Animation _setAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_anim_set);
mImageView.startAnimation(_setAnimation);
break;
default:
break;
}
}
}
@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;
}
下面是4个定义好的动画的xml:
Tween Animation动画定义在res/anim文件夹下
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0"//起始透明度 android:toAlpha="0.1"//终止透明度 android:repeatCount="5"//变换的次数 android:duration="5">//变换的时间
</alpha>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0"//开始旋转的位置 android:toDegrees="360"//结束旋转的位置,以度数为单位 android:pivotX="0"//旋转中心点的x坐标,加上%以自身为参考,加上%p以父类为参考,不加默认以自己为参考,下同 android:pivotY="0"//旋转中心点的y坐标 android:duration="3000" android:repeatCount="3">
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="0"//起始的x方向的尺寸 android:toXScale="2"//终止的x方向的尺寸 android:fromYScale="0"//起始的y方向的尺寸 android:toYScale="2"//终止的y方向上的尺寸 android:pivotX="0"//进行尺寸变换的中心点的x坐标 android:pivotY="0"//进行尺寸变换的中心点的y坐标 android:duration="3000" android:repeatCount="3">
</scale>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0"//起始x的位置 android:toXDelta="4%"//终止x的位置 android:fromYDelta="0"//起始y的位置 android:toYDelta="4%"//终止y的位置 android:duration="3000" android:repeatCount="3">
</translate>
下面是定义的一个动画集xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false">//动画插入器设置为false,即不应用加速度等效果
//参数同上面介绍相同
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="3000"/>
<rotate android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%" android:pivotY="50%" android:duration="3000"/>
<scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="0.0" android:pivotY="0.0"/>
<translate android:fromXDelta="0" android:toXDelta="500" android:fromYDelta="0" android:toYDelta="50"/>
</set>
三 Interpolator动画的插入器
插入器用于动画执行的过程中的过程效果,例如有一个加速度的效果
CycleInterpolator
AccelerateInterpolator加速插入器
DecelerateInterpolator加速插入器
AccelerateDecelerateInterpolator先加速后减速插入器
类似的效果还有其他的
使用的方法也是可以xml配置,或者动态编写:
AnimationSet _animationSet = new AnimationSet(true);
_animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
不过还是建议xml配置:
android:shareInterpolator="false"