private void performAnimate(final View target, final int start, final int end) { ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100); valueAnimator.addUpdateListener(new AnimatorUpdateListener() { // 持有一个IntEvaluator对象,方便下面估值的时候使用 private IntEvaluator mEvaluator = new IntEvaluator(); @Override public void onAnimationUpdate(ValueAnimator animator) { // 获得当前动画的进度值,整型,1-100之间 int currentValue = (Integer) animator.getAnimatedValue(); Log.d(TAG, "current value: " + currentValue); // 获得当前进度占整个动画过程的比例,浮点型,0-1之间 float fraction = animator.getAnimatedFraction(); // 直接调用整型估值器通过比例计算出宽度,然后再设给Button target.getLayoutParams().width = mEvaluator.evaluate(fraction, start, end); target.requestLayout(); } }); valueAnimator.setDuration(5000).start(); }
@Override protected void resetHeader() { final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mAnimationView.getLayoutParams(); ValueAnimator animator = ValueAnimator.ofInt(params.topMargin,(int) - getAnimationEndHeight()); animator.setEvaluator(new IntEvaluator() { @Override public Integer evaluate(float fraction, Integer startValue, Integer endValue) { int num = (Integer)super.evaluate(fraction, startValue, endValue); params.setMargins(0,num, 0, 0); mAnimationView.setLayoutParams(params); return num; } }); animator.setDuration(200); animator.start(); }
@Override protected void buildAnimator() { ObjectAnimator animator = (ObjectAnimator) getAnimator(); if (animator == null) { if (mPropertyType == PropertyDataType.UNKNOWN) { Log.w(TAG, "Property data type unknown, cannot animate."); return; } Object target = getTarget(); String propertyName = AnimationUtils.translatePropertyName(target, mPropertyName); Object actualObject = target; if (target instanceof TiViewProxy) { TiUIView intermediateObject = ((TiViewProxy) target).peekView(); if (intermediateObject != null) { actualObject = intermediateObject.getNativeView(); if (actualObject != null && propertyName.equals(PROPERTY_BACKGROUND_COLOR) && mIntValues.length == 1) { // There is no "getBackgroundColor" on Android views, // so we wrap it. actualObject = new ViewWrapper((View) actualObject); } } else { Log.w(TAG, "View not available for animation."); } } if (actualObject == null) { Log.w(TAG, "Object not available for animation (null)."); return; } switch (mPropertyType) { case FLOAT: animator = ObjectAnimator.ofFloat(actualObject, propertyName, mFloatValues); break; case INT: animator = ObjectAnimator.ofInt(actualObject, propertyName, mIntValues); break; case UNKNOWN: break; } } if (mRepeatCount != AndroidAnimation.NO_INT_VALUE) { animator.setRepeatCount(mRepeatCount); } if (mRepeatMode != AndroidAnimation.NO_INT_VALUE) { animator.setRepeatMode(mRepeatMode); } if (mEvaluator != AndroidAnimation.NO_INT_VALUE) { switch (mEvaluator) { case AndroidAnimation.INT_EVALUATOR: animator.setEvaluator(new IntEvaluator()); break; case AndroidAnimation.FLOAT_EVALUATOR: animator.setEvaluator(new FloatEvaluator()); break; case AndroidAnimation.ARGB_EVALUATOR: animator.setEvaluator(new ArgbEvaluator()); break; default: Log.w(TAG, "Evaluator set to unknown value: " + mEvaluator); } } setAnimator(animator); super.setCommonAnimatorProperties(); }