下面列出了android.animation.ValueAnimator#ofObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Enables or disables the orb color animation.
*
* <p>
* Orb color animation is handled automatically when the orb is focused/unfocused,
* however, an app may choose to override the current animation state, for example
* when an activity is paused.
* </p>
*/
public void enableOrbColorAnimation(boolean enable) {
if (mColorAnimator != null) {
mColorAnimator.end();
mColorAnimator = null;
}
if (enable) {
// TODO: set interpolator (material if available)
mColorAnimator = ValueAnimator.ofObject(mColorEvaluator,
mColors.color, mColors.brightColor, mColors.color);
mColorAnimator.setRepeatCount(ValueAnimator.INFINITE);
mColorAnimator.setDuration(mPulseDurationMs * 2);
mColorAnimator.addUpdateListener(mUpdateListener);
mColorAnimator.start();
}
}
/**
* 进场动画
*/
private void in() {
int height = AndroidUtils.getScreenParams(getContext())[1];
ValueAnimator animator = ValueAnimator.ofObject(new IntEvaluator(), -height / 2, 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override public void onAnimationUpdate(ValueAnimator animation) {
mWpm.y = (int) animation.getAnimatedValue();
mWindow.setAttributes(mWpm);
}
});
animator.setInterpolator(new BounceInterpolator()); //弹跳
Animator alpha = ObjectAnimator.ofFloat(mRootView, "alpha", 0f, 1f);
AnimatorSet set = new AnimatorSet();
set.play(animator).with(alpha);
set.setDuration(2000).start();
}
private ValueAnimator getBezierValueAnimator(LeafHolder target, RectF leafFlyRect, float progress) {
BezierEvaluator evaluator = new BezierEvaluator(getPoint1(leafFlyRect), getPoint2(leafFlyRect));
int leafFlyStartY = (int) (mCurrentProgressBounds.bottom - mLeafDrawable.getIntrinsicHeight());
int leafFlyRange = (int) (mCurrentProgressBounds.height() - mLeafDrawable.getIntrinsicHeight());
int startPointY = leafFlyStartY - mRandom.nextInt(leafFlyRange);
int endPointY = leafFlyStartY - mRandom.nextInt(leafFlyRange);
ValueAnimator animator = ValueAnimator.ofObject(evaluator,
new PointF((int) (leafFlyRect.right - mLeafDrawable.getIntrinsicWidth()), startPointY),
new PointF(leafFlyRect.left, endPointY));
animator.addUpdateListener(new BezierListener(target));
animator.setTarget(target);
animator.setDuration((long) ((mRandom.nextInt(300) + mDuration * DEFAULT_LEAF_FLY_DURATION_FACTOR) * (1.0f - progress)));
return animator;
}
private void startAnimation() {
final ImageView imageView = findViewById(R.id.image_view);
ValueAnimator anim = ValueAnimator.ofObject(new ArgbEvaluator(), RED,
BLUE);
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView.setBackgroundColor((Integer) animation
.getAnimatedValue());
}
});
anim.setDuration(10000);
anim.start();
}
private static void applyColorToView(final TextView textView, int color, boolean fromCache) {
if (fromCache) {
textView.setTextColor(color);
} else {
Integer colorFrom = textView.getCurrentTextColor();
Integer colorTo = color;
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
textView.setTextColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.start();
}
}
private void invalidateField(final EditText editText){
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), getResources().getColor(R.color.colorHeader), getResources().getColor(R.color.invalidRed));
colorAnimation.setDuration(200);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
editText.getBackground().setColorFilter((int) animation.getAnimatedValue(), PorterDuff.Mode.SRC_ATOP);
editText.setTextColor((int) animation.getAnimatedValue());
}
});
Animation invalidShake = AnimationUtils.loadAnimation(this, R.anim.invalid_shake);
invalidShake.setInterpolator(new CycleInterpolator(7F));
colorAnimation.start();
editText.startAnimation(invalidShake);
}
private void start() {
if (!isEnableAnim) { // 禁止动画
setText(mPrefixString + format(new BigDecimal(mNumEnd)) + mPostfixString);
return;
}
ValueAnimator animator = ValueAnimator.ofObject(new BigDecimalEvaluator(), new BigDecimal(mNumStart), new BigDecimal(mNumEnd));
animator.setDuration(mDuration);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
BigDecimal value = (BigDecimal) valueAnimator.getAnimatedValue();
setText(mPrefixString + format(value) + mPostfixString);
}
});
animator.start();
}
/**创建动画*/
@Override
protected ValueAnimator initAnim() {
if(null!=fenbuData) {
ValueAnimator anim = ValueAnimator.ofObject(new AngleEvaluator(), 0f, 1f);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
return anim;
}
return null;
}
/**创建动画*/
@Override
protected ValueAnimator initAnim() {
anim = ValueAnimator.ofObject(new AngleEvaluator(), 0f, 1.0f);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
return anim;
}
private ValueAnimator getFocusAnimation(int fromColor, int toColor) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
fromColor,
toColor);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
mFloatingLabel.setTextColor((Integer) animator.getAnimatedValue());
}
});
return colorAnimation;
}
private void moveAnimate(Point start, Point end) {
ValueAnimator valueAnimator = ValueAnimator.ofObject(new MoveEvaluator(), start, end);
valueAnimator.setInterpolator(new DecelerateInterpolator());
MoveAnimation moveAnimation = new MoveAnimation();
valueAnimator.addUpdateListener(moveAnimation);
valueAnimator.setDuration(400);
valueAnimator.start();
}
public void animateStatusActionBar(int colorFrom,int colorTo){
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(250); // milliseconds
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
getWindow().setStatusBarColor((int) animator.getAnimatedValue());
getSupportActionBar().setBackgroundDrawable(new ColorDrawable((int) animator.getAnimatedValue()));
}
});
colorAnimation.start();
}
public CommonTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
setClipChildren(false);
setClipToPadding(false);
this.mContext = context;
mTabsContainer = new LinearLayout(context);
addView(mTabsContainer);
obtainAttributes(context, attrs);
//get layout_height
String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
//create ViewPager
if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
} else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
} else {
int[] systemAttrs = {android.R.attr.layout_height};
TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
a.recycle();
}
mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP);
mValueAnimator.addUpdateListener(this);
}
private void startTextColorAnimation(final TextView view, long startDelay, long duration, int startColor, int endColor){
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor);
colorAnimation.setDuration(duration);
colorAnimation.setStartDelay(startDelay);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
view.setTextColor((int) animation.getAnimatedValue());
}
});
colorAnimation.start();
}
public CommonTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
setClipChildren(false);
setClipToPadding(false);
this.mContext = context;
mTabsContainer = new LinearLayout(context);
addView(mTabsContainer);
obtainAttributes(context, attrs);
//get layout_height
String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
//create ViewPager
if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
} else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
} else {
int[] systemAttrs = {android.R.attr.layout_height};
TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
a.recycle();
}
mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP);
mValueAnimator.addUpdateListener(this);
}
private void colorize(int i) {
if (valueAnimator != null) valueAnimator.cancel();
valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), android.R.color.transparent, i);
valueAnimator.addUpdateListener(animation -> {
GradientDrawable drawable = new DrawableGradient(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{(int) animation.getAnimatedValue(), android.R.color.transparent}, 0);
if (colorBackground != null) {
colorBackground.setBackground(drawable);
}
});
valueAnimator.setDuration(ViewUtil.PHONOGRAPH_ANIM_TIME).start();
}
public CommonTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
setClipChildren(false);
setClipToPadding(false);
this.mContext = context;
mTabsContainer = new LinearLayout(context);
addView(mTabsContainer);
obtainAttributes(context, attrs);
//get layout_height
String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
//create ViewPager
if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + "")) {
} else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + "")) {
} else {
int[] systemAttrs = {android.R.attr.layout_height};
TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
a.recycle();
}
mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP);
mValueAnimator.addUpdateListener(this);
}
@Override
public Animator createAnimator(ViewGroup sceneRoot,
TransitionValues startValues, TransitionValues endValues) {
// This transition can only be applied to views that are on both starting and ending scenes.
if (null == startValues || null == endValues) {
return null;
}
// Store a convenient reference to the target. Both the starting and ending layout have the
// same target.
final View view = endValues.view;
// Store the object containing the background property for both the starting and ending
// layouts.
Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
// This transition changes background colors for a target. It doesn't animate any other
// background changes. If the property isn't a ColorDrawable, ignore the target.
if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
ColorDrawable startColor = (ColorDrawable) startBackground;
ColorDrawable endColor = (ColorDrawable) endBackground;
// If the background color for the target in the starting and ending layouts is
// different, create an animation.
if (startColor.getColor() != endColor.getColor()) {
// Create a new Animator object to apply to the targets as the transitions framework
// changes from the starting to the ending layout. Use the class ValueAnimator,
// which provides a timing pulse to change property values provided to it. The
// animation runs on the UI thread. The Evaluator controls what type of
// interpolation is done. In this case, an ArgbEvaluator interpolates between two
// #argb values, which are specified as the 2nd and 3rd input arguments.
ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(),
startColor.getColor(), endColor.getColor());
// Add an update listener to the Animator object.
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Object value = animation.getAnimatedValue();
// Each time the ValueAnimator produces a new frame in the animation, change
// the background color of the target. Ensure that the value isn't null.
if (null != value) {
view.setBackgroundColor((Integer) value);
}
}
});
// Return the Animator object to the transitions framework. As the framework changes
// between the starting and ending layouts, it applies the animation you've created.
return animator;
}
}
// For non-ColorDrawable backgrounds, we just return null, and no animation will take place.
return null;
}
@Override
public void onEnterScreen() {
if (itemAnimationEnabled && weather != null) {
ValueAnimator timeDay = ValueAnimator.ofObject(new FloatEvaluator(), startTimes[0], currentTimes[0]);
timeDay.addUpdateListener(animation -> {
animCurrentTimes[0] = (Float) animation.getAnimatedValue();
sunMoonView.setTime(startTimes, endTimes, animCurrentTimes);
});
double totalRotationDay = 360.0 * 7 * (currentTimes[0] - startTimes[0]) / (endTimes[0] - startTimes[0]);
ValueAnimator rotateDay = ValueAnimator.ofObject(
new FloatEvaluator(), 0, (int) (totalRotationDay - totalRotationDay % 360)
);
rotateDay.addUpdateListener(animation ->
sunMoonView.setDayIndicatorRotation((Float) animation.getAnimatedValue())
);
attachAnimatorSets[0] = new AnimatorSet();
attachAnimatorSets[0].playTogether(timeDay, rotateDay);
attachAnimatorSets[0].setInterpolator(new OvershootInterpolator(1f));
attachAnimatorSets[0].setDuration(getPathAnimatorDuration(0));
attachAnimatorSets[0].start();
ValueAnimator timeNight = ValueAnimator.ofObject(new FloatEvaluator(), startTimes[1], currentTimes[1]);
timeNight.addUpdateListener(animation -> {
animCurrentTimes[1] = (Float) animation.getAnimatedValue();
sunMoonView.setTime(startTimes, endTimes, animCurrentTimes);
});
double totalRotationNight = 360.0 * 4 * (currentTimes[1] - startTimes[1]) / (endTimes[1] - startTimes[1]);
ValueAnimator rotateNight = ValueAnimator.ofObject(
new FloatEvaluator(), 0, (int) (totalRotationNight - totalRotationNight % 360)
);
rotateNight.addUpdateListener(animation ->
sunMoonView.setNightIndicatorRotation(-1 * (Float) animation.getAnimatedValue())
);
attachAnimatorSets[1] = new AnimatorSet();
attachAnimatorSets[1].playTogether(timeNight, rotateNight);
attachAnimatorSets[1].setInterpolator(new OvershootInterpolator(1f));
attachAnimatorSets[1].setDuration(getPathAnimatorDuration(1));
attachAnimatorSets[1].start();
if (phaseAngle > 0) {
ValueAnimator moonAngle = ValueAnimator.ofObject(new FloatEvaluator(), 0, phaseAngle);
moonAngle.addUpdateListener(animation ->
phaseView.setSurfaceAngle((Float) animation.getAnimatedValue())
);
attachAnimatorSets[2] = new AnimatorSet();
attachAnimatorSets[2].playTogether(moonAngle);
attachAnimatorSets[2].setInterpolator(new DecelerateInterpolator());
attachAnimatorSets[2].setDuration(getPhaseAnimatorDuration());
attachAnimatorSets[2].start();
}
}
}
public static ValueAnimator createBackgroundAnimation(int argbStart, int argbEnd) {
return ValueAnimator.ofObject(ArgbEvaluator.getInstance(), argbStart, argbEnd);
}