您的位置:首页 > 移动开发 > Objective-C

强大的 Android 属性动画 ObjectAnimator

2015-06-02 21:26 696 查看
转载自:http://blog.csdn.net/guolin_blog/article/details/43536355

单一动画

例如我要移动 textView 当前位置移出屏幕,再移回初始位置
float curTranslationX = textview.getTranslationX();
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", curTranslationX, -500f, curTranslationX);
animator.setDuration(5000);
animator.start();
补充点原文里没有的
ObjectAnimator.ofFloat(要操作的对象,操作类型,动画起始位置,动画中间位置,。。。动画中间位置,动画结束位置);
注意:这里的所有的位置都是 距离 操作对象 的布局位置 的 偏移值。比如位移动画translation,ObjectAnimator.ofFloat(textview, "translationX",100,300);从 X布局位置 +100 的位置开始 到  X布局位置+300  的位置结束;下面举个例子,有两个button,点击button1,View Y轴往上移动200,再点击button2,View Y轴回到原位。第一个动画很简单:
ObjectAnimator At1 = ObjectAnimator.ofFloat(slideView,"translationY",slideView.getTranslationY(),-200f);At1.setDuration(3000);
At1.start();
第二个动画:
ObjectAnimator At2 = ObjectAnimator.ofFloat(v, "translationY",slideView.getTranslationY() ,???);At2.setDuration(3000);At2.start();
这个???该是多少,200?错了,应该是 0f ,上面说了,这个???的值是 slideView的  实际位置+???slideView 的布局位置用absY表示第一个动画移动到了 slideView 的 absY-200f位置, 界面上看 slideView 位置变化了,但是 absY 并没有变,要回到初始位置, 那么就是 absY+0 咯,所以写0f 就好了。slideView.getTranslationY(),得到当前位置距 absY 的偏移量,如果动画开始位置就是当前偏移的位置,那么这个可以省略,例如上面的例子可以这样写
ObjectAnimator At2 = ObjectAnimator.ofFloat(v, "translationY",???);At2.setDuration(3000);At2.start();
简单粗暴。

组合动画

旋转和淡入淡出同时进行,结束后进行位移动画
ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);AnimatorSet animSet = new AnimatorSet();animSet.play(rotate).with(fadeInOut).after(moveIn);animSet.setDuration(5000);animSet.start();

动画监听

动画的值更新监听器

anim.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {// TODO Auto-generated method stub}});
anim.addListener(new AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {}@Overridepublic void onAnimationCancel(Animator animation) {}});
只想监听一个事件?看下面
anim.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {}});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息