简介: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();
- static {
- PROXY_PROPERTIES.put("alpha", PreHoneycombCompat.ALPHA);
- PROXY_PROPERTIES.put("pivotX", PreHoneycombCompat.PIVOT_X);
- PROXY_PROPERTIES.put("pivotY", PreHoneycombCompat.PIVOT_Y);
- PROXY_PROPERTIES.put("translationX", PreHoneycombCompat.TRANSLATION_X);
- PROXY_PROPERTIES.put("translationY", PreHoneycombCompat.TRANSLATION_Y);
- PROXY_PROPERTIES.put("rotation", PreHoneycombCompat.ROTATION);
- PROXY_PROPERTIES.put("rotationX", PreHoneycombCompat.ROTATION_X);
- PROXY_PROPERTIES.put("rotationY", PreHoneycombCompat.ROTATION_Y);
- PROXY_PROPERTIES.put("scaleX", PreHoneycombCompat.SCALE_X);
- PROXY_PROPERTIES.put("scaleY", PreHoneycombCompat.SCALE_Y);
- PROXY_PROPERTIES.put("scrollX", PreHoneycombCompat.SCROLL_X);
- PROXY_PROPERTIES.put("scrollY", PreHoneycombCompat.SCROLL_Y);
- PROXY_PROPERTIES.put("x", PreHoneycombCompat.X);
- PROXY_PROPERTIES.put("y", PreHoneycombCompat.Y);
- }
2)定义动画组
1.
ObjectAnimator animDown=ObjectAnimator.ofFloat(balls.get(2), "y",0f,getHeight()-balls.get(2).getHeight()).setDuration(500);
2.
ObjectAnimator animUp=ObjectAnimator.ofFloat(balls.get(2), "y",getHeight()-balls.get(2).getHeight(),0f).setDuration(500);
3.
AnimatorSet s1=new AnimatorSet();
使动画时间开始一致 animation.playTogether(anim1,anim2,s1);
3)值动画(AnimatorInflater布局加载器)
1.
ValueAnimator alphaAnimator=(ValueAnimator) AnimatorInflater.loadAnimator(AnimationLoading.this,R.anim.animator);
2.
alphaAnimator.setTarget(balls.get(1));
3.
alphaAnimator.addUpdateListener(new AnimatorUpdateListener() {
4.
@Override
5.
public void onAnimationUpdate(ValueAnimator animation) {
6.
balls.get(1).setAlpha((Float) animation.getAnimatedValue());
7.
}
8.
});
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)
//定义各种属性>汇总
1.
PropertyValuesHolder animY=PropertyValuesHolder.ofFloat("y",balls.get(1).getY(),getHeight()-100);
2.
PropertyValuesHolder alpha=PropertyValuesHolder.ofFloat("alpha",1.0f,.5f);
3.
ObjectAnimator pvhAlpha=ObjectAnimator.ofPropertyValuesHolder(balls.get(1), animY,alpha).setDuration(1000);
1.
PropertyValuesHolder widthHolder=PropertyValuesHolder.ofFloat("width",ball.getWidth(),ball.getWidth()*2);
2.
PropertyValuesHolder heightHolder=PropertyValuesHolder.ofFloat("height",ball.getHeight(),ball.getHeight()*2);
3.
PropertyValuesHolder xPt=PropertyValuesHolder.ofFloat("x",ball.getX(),ball.getX()-BALL_SIZE/2f);
4.
PropertyValuesHolder yPt=PropertyValuesHolder.ofFloat("y",ball.getY(),ball.getY()-BALL_SIZE/2f);
5.
ObjectAnimator sumAnimator=ObjectAnimator.ofPropertyValuesHolder(ball,widthHolder,heightHolder,xPt,yPt).setDuration(750);
6.
sumAnimator.setRepeatMode(ValueAnimator.REVERSE);
7.
sumAnimator.setRepeatCount(1);//设置repeatCount=1使其恢复原样
bounceAnim.reverse();
***************************************ObjectAnimator组件应用*************************************
位移:移动的单位为像素,可以指定一系列的位置
1.
ObjectAnimator.ofFloat(target,"translationX",0,50).setDuration(duration).start();
2.
ObjectAnimator.ofFloat(target,"translationY",0,50,-50,0).setDuration(duration).start();
1.
ObjectAnimator.ofFloat(target,"scaleX",1,2,1).setDuration(duration).start();
2.
ObjectAnimator.ofFloat(target,"scaleY",1,2).setDuration(duration).start();
1.
ObjectAnimator.ofFloat(target,"alpha",1,0,1).setDuration(duration).start();
1.
ObjectAnimator.ofFloat(target,"rotationX",0,180,0).setDuration(duration).start();
2.
ObjectAnimator.ofFloat(target,"rotationY",0,360).setDuration(duration).start();
3.
ObjectAnimator.ofFloat(target,"rotation",0,180,0).setDuration(duration).start();
1.
ViewHelper.setPivotX(target,target.getWidth()/2);
2.
ViewHelper.setPivotY(target,target.getHeight()/2);
***************************************ViewPropertyAnimator组件应用*************************************
1.
animate(target).setDuration(2000);
2.
animate(animatingButton).alpha(0);
3.
animate(animatingButton).x(xValue).y(yValue);
4.
animate(animatingButton).rotationYBy(720);
开源地址: https://github.com/JakeWharton/NineOldAndroids
其他用法:
Android动画进阶—使用开源动画库nineoldandroids
本文深入解析了支持低版本开发的Android动画框架NineOldAndroids,包括ObjectAnimator、ViewAnimator等API,解决Tween动画问题,并详细介绍了创建动画、动画组、动画集及动画集的应用方法。
440

被折叠的 条评论
为什么被折叠?



