标签:objectanimator valueanimator 属性动画 animator android
// 通过静态方法构建一个ObjectAnimator对象 // 设置作用对象、属性名称、数值集合 ObjectAnimator.ofFloat(view, "translationX", 0.0F, 200.0F) // 设置执行时间(1000ms) .setDuration(1000) // 开始动画 .start();
// 修改view的y属性, 从当前位置移动到300.0f ObjectAnimator yBouncer = ObjectAnimator.ofFloat(view, "y", view.getY(), 300.0f); yBouncer.setDuration(1500); // 设置插值器(用于调节动画执行过程的速度) yBouncer.setInterpolator(new BounceInterpolator()); // 设置重复次数(缺省为0,表示不重复执行) yBouncer.setRepeatCount(1); // 设置重复模式(RESTART或REVERSE),重复次数大于0或INFINITE生效 yBouncer.setRepeatMode(ValueAnimator.REVERSE); // 设置动画开始的延时时间(200ms) yBouncer.setStartDelay(200); // 开始动画 yBouncer.start();
// 通过静态方法构建一个ValueAnimator对象 // 设置数值集合 ValueAnimator animator = ValueAnimator.ofFloat(0f, 200.0f); // 设置作用对象 animator.setTarget(view); // 设置执行时间(1000ms) animator.setDuration(1000); // 添加动画更新监听 animator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // 获取当前值 Float mValue = (Float) animation.getAnimatedValue(); // 设置横向偏移量 view.setTranslationX(mValue); // 设置纵向偏移量 view.setTranslationY(mValue); } }); // 开始动画 animator.start();
// 获取view左边位置 int left = view.getLeft(); // 获取view右边位置 int right = view.getRight(); // 将view左边增加10像素 PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", left, left + 10); // 将view右边减少10像素 PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right", right, right - 10); // 在X轴缩放从原始比例1f,缩小到最小0f,再放大到原始比例1f PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f); // 在Y轴缩放从原始比例1f,缩小到最小0f,再放大到原始比例1f PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f); // 将PropertyValuesHolder交付给ObjectAnimator进行构建 ObjectAnimator customAnim = ObjectAnimator.ofPropertyValuesHolder(view, pvhLeft, pvhRight, pvhScaleX, pvhScaleY); // 设置执行时间(1000ms) customAnim.setDuration(1000); // 开始动画 customAnim.start();
// 设置在动画开始时,旋转角度为0度 Keyframe kf0 = Keyframe.ofFloat(0f, 0f); // 设置在动画执行50%时,旋转角度为360度 Keyframe kf1 = Keyframe.ofFloat(.5f, 360f); // 设置在动画结束时,旋转角度为0度 Keyframe kf2 = Keyframe.ofFloat(1f, 0f); // 使用PropertyValuesHolder进行属性名称和值集合的封装 PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe( "rotation", kf0, kf1, kf2); // 通过ObjectAnimator进行执行 ObjectAnimator.ofPropertyValuesHolder(view, pvhRotation) // 设置执行时间(1000ms) .setDuration(1000) // 开始动画 .start();
AnimatorSet bouncer = new AnimatorSet(); bouncer.play(bounceAnim).before(squashAnim1); bouncer.play(squashAnim1).with(squashAnim2); bouncer.play(squashAnim1).with(stretchAnim1); bouncer.play(squashAnim1).with(stretchAnim2); bouncer.play(bounceBackAnim).after(stretchAnim2); ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); fadeAnim.setDuration(250); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(bouncer).before(fadeAnim); animatorSet.start();
// 1. 在回调中手动更新View对应属性: AnimatorUpdateListener l = new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { // 当前的分度值范围为0.0f->1.0f // 分度值是动画执行的百分比。区别于AnimatedValue。 float fraction = animation.getAnimatedFraction(); // 以下的的效果为 View从完全透明到不透明, view.setAlpha(fraction); // Y方向向下移动300px的距离. view.setTranslationY(fraction * 300.0f); } }; ValueAnimator mAnim = ValueAnimator.ofFloat(0f, 1.0f); mAnim.addUpdateListener(l); mAnim.setDuration(1000); mAnim.start();
// 2. 在自定义View内部用于引发重绘 public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener { public MyAnimationView(Context context) { super(context); } @Override public void onAnimationUpdate(ValueAnimator animation) { // 手动触发界面重绘 invalidate(); } }
<span style="font-weight: 700;"> </span>// 将view透明度从当前的1.0f更新为0.5f,在动画结束时移除该View ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 0.5f); anim.setDuration(1000); anim.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { // 动画开始时调用 } @Override public void onAnimationRepeat(Animator animation) { // 动画重复时调用 } @Override public void onAnimationEnd(Animator animation) { // 动画结束时调用 ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } @Override public void onAnimationCancel(Animator animation) { // 动画取消时调用 } }); anim.start();
<set android:ordering=["together" ¦ "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float¦ int ¦ color" android:valueTo="float¦ int ¦ color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat"¦ "reverse"] android:valueType=["intType"¦ "floatType"]/> <animator android:duration="int" android:valueFrom="float¦ int ¦ color" android:valueTo="float¦ int ¦ color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat"¦ "reverse"] android:valueType=["intType"¦ "floatType"]/> <set> ... </set> </set>
<span style="font-weight: 700;"> </span> // 加载xml属性动画 Animator anim = AnimatorInflater .loadAnimator(this, R.anim.animator_set); anim.setTarget(view); anim.start();
<?xml version="1.0" encoding="utf-8"?> <set> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueTo="200" android:valueType="floatType" android:propertyName="x" android:repeatCount="1" android:repeatMode="reverse"/> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueTo="400" android:valueType="floatType" android:propertyName="y" android:repeatCount="1" android:repeatMode="reverse"/> </set>
<span style="font-weight: 700;"> </span>// 类型估值 - 抛物线示例 TypeEvaluator<PointF> typeEvaluator = new TypeEvaluator<PointF>() { @Override public PointF evaluate(float fraction, PointF startValue, PointF endValue) { float time = fraction * 3; Log.e(TAG, time + ""); // x方向200px/s ,y方向0.5 * 200 * t * t PointF point = new PointF(); point.x = 120 * time; point.y = 0.5f * 200 * time * time; return point; } }; ValueAnimator valueAnimator = ValueAnimator.ofObject(typeEvaluator, new PointF(0, 0)); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.setDuration(3000); valueAnimator.start(); valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { PointF point = (PointF) animation.getAnimatedValue(); view.setX(point.x); view.setY(point.y); } });
标签:objectanimator valueanimator 属性动画 animator android
原文地址:http://blog.csdn.net/poplartang/article/details/44036455