Tween动画(补间动画)
Frame动画(帧动画)
属性动画
1. Tween动画
Tween动画(补间动画), 在anim文件夹定义动画
参考帖子
Tween动画 aniam实现 补间动画(Tween)的实现 (aniam下xml文件实现的)
Android开发之动画效果浅析(一)
Animation,但都是在同一个时间执行的,是并行,不是串行执行的。
“如果AnimationSet中有一些设定,如duration,fillBefore等,它包含的子动作也设定了的话,
“子动作中的设定将会给覆盖掉。
//缩放动画
private void scaleAnimation(){
Toast.makeText(this,"缩放动画",Toast.LENGTH_SHORT).show();
//Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f 图像缩放中心
ScaleAnimation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(1000);
imageView.startAnimation(animation);
}
// Animation.RELATIVE_TO_PARENT, 0.5f, //0.5 = 1/2的自己父控件的长度
//Animation.RELATIVE_TO_SELF, 0.5f);//0.5 = 1/2的自己的长度
//透明动画
private void alphaAnimation(){
//设置渐变从不透明->透明,1表示不透明,0表示透明
Toast.makeText(this,"透明动画",Toast.LENGTH_SHORT).show();
AlphaAnimation animation = new AlphaAnimation(1,0.1f);
animation.setDuration(1000);
animation.setFillAfter(true);
imageView.startAnimation(animation);
}
//旋转动画
private void rotateAnimation(){
Toast.makeText(this,"旋转动画",Toast.LENGTH_SHORT).show();
//Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f 图像旋转中心
RotateAnimation animation =new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(300);//设置动画持续时间为3秒
//animation.setRepeatCount(8);//设置重复次数
//animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
//animation.setStartOffset(long startOffset);//执行前的等待时间
imageView.startAnimation(animation);
}
//移动动画
private void translateAnimation(){
Toast.makeText(this,"移动动画",Toast.LENGTH_SHORT).show();
if (!again){
TranslateAnimation animation = new TranslateAnimation(0,200,0,0);
again = true;
animation.setDuration(1000);
animation.setFillAfter(true);
imageView.startAnimation(animation);
}else {
TranslateAnimation animation = new TranslateAnimation(0,0,0,200);
animation.setDuration(1000);
animation.setFillAfter(true);
imageView.startAnimation(animation);
again = false;
}
}
private void animationTogther(){
Toast.makeText(this,"综合动画",Toast.LENGTH_SHORT).show();
ScaleAnimation animation1 = new ScaleAnimation(1.0f, 0.2f, 1.0f, 0.2f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //渐变
RotateAnimation animation2 =new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //旋转
TranslateAnimation animation3 = new TranslateAnimation(0,200,0,0); //移动
AlphaAnimation animation4 = new AlphaAnimation(1,0.3f); //透明
AnimationSet set = new AnimationSet(true);
set.setDuration(2000);
set.setFillAfter(true);
set.addAnimation(animation1);
set.addAnimation(animation2);
set.addAnimation(animation3);
set.addAnimation(animation4);
imageView.startAnimation(set);
}
2.Frame动画
Android 用Animation-list实现逐帧动画 点击进入查看详情
3. 属性动画(重点)
private void objectSequent1(){
Toast.makeText(this, "在一个ObjectAnimator实现", Toast.LENGTH_SHORT).show();
ObjectAnimator anim = ObjectAnimator//
.ofFloat(imageView, "zhy", 0.0F, 1.0F)//
.setDuration(500);//
anim.start();
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
float cVal = (Float) animation.getAnimatedValue();
imageView.setAlpha(cVal);
imageView.setScaleX(cVal);
imageView.setScaleY(cVal);
}
});
}
private void objectSequent2(){
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);
animator.setDuration(2000);
animator.start();
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Toast.makeText(AnimalActivity.this, "动画监听结束", Toast.LENGTH_SHORT).show();
}
});
/* animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Toast.makeText(AnimalActivity.this, "动画监听开始", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(AnimalActivity.this, "动画监听结束", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});*/
}
private void objectSequent3(){
}
//----------------ObjectAnimator异步执行的动画------------------------------------------------------
/**
* ------- 此动画改变了动画的属性------
* 第二个参数的属性有
* translationX X轴平移
* translationY Y轴平移
* X Y 与xml TranslateAnimation设置的概念一样。
* rotation rotationX rotationY
* scaleX,scaleY,alpha
* */
private void translateObject(){
Toast.makeText(this, "移动动画", Toast.LENGTH_SHORT).show();
if (!again){
ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F).setDuration(1000).start();
again = true;
}else {
ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F).setDuration(1000).start();
again = false;
}
}
private void RotateObject(){
ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(5000).start();
}
private void alphaObject(){
ObjectAnimator.ofFloat(imageView, "alpha", 0F, 1.0F).setDuration(5000).start();
}
private void scaleObject(){
ObjectAnimator.ofFloat(imageView, "scaleX", 0F, 1.0F).setDuration(5000).start();
}
private void objectTogther(){
ObjectAnimator animator0=ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F);
ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);
ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView, "alpha", 0F, 1.0F);
ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView, "scaleX", 0F, 1.0F);
ObjectAnimator animator4=ObjectAnimator.ofFloat(imageView, "scaleY", 0F, 1.0F);
AnimatorSet set =new AnimatorSet();
//交叉执行
set.play(animator0).with(animator1);
set.play(animator2).with(animator3).with(animator4).after(animator0);
//同时执行的
//set.playTogether(animator0,animator1,animator2,animator3,animator4);
//按顺序执行的
//set.playSequentially(animator0,animator1,animator2,animator3,animator4);
set.setDuration(2000);
set.start();
}
private void objectSequent(){
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0F, 200F);
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("rotation", 0F, 360F);
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("alpha", 0F, 1.0F);
PropertyValuesHolder p4 = PropertyValuesHolder.ofFloat("scaleX", 0F, 1.0F);
PropertyValuesHolder p5 = PropertyValuesHolder.ofFloat("scaleY", 0F, 1.0F);
ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3,p4,p5).setDuration(2000).start();
}
知识点总结:
常用属性: translationX translationY X Y 与xml TranslateAnimation设置的概念一样。 * rotation rotationX rotationY *
scaleX,scaleY *alpha
常用类:
setInterpolator插值器
AnimatorUpdateListener
AnimatorListenerAdapter
AnimatorListener
valueAnimator 没有动画效果,和AnimatorUpdateListener结合使用何以实现属性值改变 TypeEvaluator