首页 > 代码库 > Android开源-NineOldAndroids

Android开源-NineOldAndroids

开源地址: https://github.com/JakeWharton/NineOldAndroids

简介:NineOldAndroids是一款支持在低版本开发的Android动画的框架 包括了一系列如ViewAnimator,ObjectAnimator,

ViewPropertyAnimator等API,解决了Tween动画中移动过程只显示移动效果,而不是真正组件的问题.


1)创建ObjectAnimator
ObjectAnimator anim1=ObjectAnimator.ofFloat(balls.get(0),"y",0f,getHeight()-balls.get(0).getHeight()).setDuration(500);
调用开始 animation.start();
克隆 ObjectAnimator anim2=anim1.clone();


2)定义动画组

ObjectAnimator animDown=ObjectAnimator.ofFloat(balls.get(2), "y",0f,getHeight()-balls.get(2).getHeight()).setDuration(500);
ObjectAnimator animUp=ObjectAnimator.ofFloat(balls.get(2), "y",getHeight()-balls.get(2).getHeight(),0f).setDuration(500);
AnimatorSet s1=new AnimatorSet();
使动画具有连贯性 s1.playSequentially(animDown,animUp);
使动画时间开始一致 animation.playTogether(anim1,anim2,s1);

3)值动画(AnimatorInflater布局加载器)

ValueAnimator alphaAnimator=(ValueAnimator) AnimatorInflater.loadAnimator(AnimationLoading.this,R.anim.animator);
alphaAnimator.setTarget(balls.get(1));
		alphaAnimator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				balls.get(1).setAlpha((Float) animation.getAnimatedValue());
			}
		});


4)动画集
AnimatorSet s1=(AnimatorSet) AnimatorInflater.loadAnimator(AnimationLoading.this,R.anim.animator_set);
s1.setTarget(balls.get(2));
对象动画(移动/颜色改变)
ObjectAnimator s2=(ObjectAnimator) AnimatorInflater.loadAnimator(AnimationLoading.this, R.anim.color_animator);
s2.setTarget(balls.get(3));
定义动画顺序
((AnimatorSet)animation).play(animX).before(animY);//animX在animY前面
((AnimatorSet)animation).play(animX).with(animY);//animX与animY同步执行
//圆弧加速器
new CycleInterpolator(2.0f)


//定义各种属性>汇总
PropertyValuesHolder animY=PropertyValuesHolder.ofFloat("y",balls.get(1).getY(),getHeight()-100);
PropertyValuesHolder alpha=PropertyValuesHolder.ofFloat("alpha",1.0f,.5f);
ObjectAnimator pvhAlpha=ObjectAnimator.ofPropertyValuesHolder(balls.get(1), animY,alpha).setDuration(1000);
//设置放大动画
PropertyValuesHolder widthHolder=PropertyValuesHolder.ofFloat("width",ball.getWidth(),ball.getWidth()*2);
PropertyValuesHolder heightHolder=PropertyValuesHolder.ofFloat("height",ball.getHeight(),ball.getHeight()*2);
PropertyValuesHolder xPt=PropertyValuesHolder.ofFloat("x",ball.getX(),ball.getX()-BALL_SIZE/2f);
PropertyValuesHolder yPt=PropertyValuesHolder.ofFloat("y",ball.getY(),ball.getY()-BALL_SIZE/2f);
ObjectAnimator sumAnimator=ObjectAnimator.ofPropertyValuesHolder(ball,widthHolder,heightHolder,xPt,yPt).setDuration(750);
sumAnimator.setRepeatMode(ValueAnimator.REVERSE);
sumAnimator.setRepeatCount(1);//设置repeatCount=1使其恢复原样
//转换动画的轨迹
bounceAnim.reverse();

***************************************ObjectAnimator组件应用*************************************

位移:移动的单位为像素,可以指定一系列的位置
ObjectAnimator.ofFloat(target,"translationX",0,50).setDuration(duration).start();
ObjectAnimator.ofFloat(target,"translationY",0,50,-50,0).setDuration(duration).start();
缩放:1.0f代表为原来长/宽度的1倍,同理其他.所有的倍数都是因最早设定的宽度成倍
ObjectAnimator.ofFloat(target,"scaleX",1,2,1).setDuration(duration).start();
ObjectAnimator.ofFloat(target,"scaleY",1,2).setDuration(duration).start();
透明度:1.0f表示不透明 0表示全透明
ObjectAnimator.ofFloat(target,"alpha",1,0,1).setDuration(duration).start();
旋转:
ObjectAnimator.ofFloat(target,"rotationX",0,180,0).setDuration(duration).start();
ObjectAnimator.ofFloat(target,"rotationY",0,360).setDuration(duration).start();
ObjectAnimator.ofFloat(target,"rotation",0,180,0).setDuration(duration).start();
设置变换中心;当然,多个动画可以组合变换
ViewHelper.setPivotX(target,target.getWidth()/2);
ViewHelper.setPivotY(target,target.getHeight()/2);

***************************************ViewPropertyAnimator组件应用*************************************
animate(target).setDuration(2000);
animate(animatingButton).alpha(0);
animate(animatingButton).x(xValue).y(yValue);
animate(animatingButton).rotationYBy(720);


Android开源-NineOldAndroids