下面列出了android.animation.ValueAnimator#setFloatValues ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void animateStageOut() {
final ValueAnimator animator = new ValueAnimator();
animator.setFloatValues(1F, 0F);
animator.setInterpolator(new FastOutSlowInInterpolator());
animator.setDuration(ANIMATION_DURATION);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
targetParent.removeView(stage);
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float currentAlpha = (float) valueAnimator.getAnimatedValue();
stage.setAlpha(currentAlpha);
}
});
animator.start();
}
private void startSunShadow() {
isDrawSunShadow=true;
ValueAnimator valueAnimator=animMap.get(ANIM_WEATHER_SHADOW);
if (valueAnimator==null){
valueAnimator=ValueAnimator.ofFloat().setDuration(400);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
sunShadowWidth= (float) animation.getAnimatedValue();
}
});
animMap.put(ANIM_WEATHER_SHADOW,valueAnimator);
}
valueAnimator.setFloatValues(0,getMeasuredWidth(),getMeasuredWidth()*0.8f);
startValueAnimator(valueAnimator);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public static ValueAnimator animateMarkerToHC(final MapView map, final Marker marker, final GeoPoint finalPosition, final GeoPointInterpolator GeoPointInterpolator) {
final GeoPoint startPosition = marker.getPosition();
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float v = animation.getAnimatedFraction();
GeoPoint newPosition = GeoPointInterpolator.interpolate(v, startPosition, finalPosition);
marker.setPosition(newPosition);
map.invalidate();
}
});
valueAnimator.setFloatValues(0, 1); // Ignored.
valueAnimator.setDuration(3000);
valueAnimator.start();
return valueAnimator;
}
private void runAnimation(final ValueAnimator animator, final float startValue,
final float targetValue, final long duration_ms,
final TimeInterpolator interpolator)
{
if (startValue == targetValue)
{
return;
}
animator.setInterpolator(interpolator);
animator.setDuration(duration_ms);
animator.setFloatValues(startValue, targetValue);
animator.start();
}
private void init() {
mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mDecorView = (ViewGroup) mContext.getWindow().getDecorView();
mPopup = new PopupLayer(mContext, mRadius);
mPopup.setVisibility(INVISIBLE);
mAlphAnimator = new ValueAnimator();
mAlphAnimator.setFloatValues(0.0f, 1.0f);
mAlphAnimator.setDuration(mAnimDuration);
mAlphAnimator.setInterpolator(new LinearOutSlowInInterpolator());
mAlphAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mPopup.setShadowViewAlpha(Float.valueOf(animation.getAnimatedValue() + ""));
}
});
addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
initButtons();
if (onButtonPreparedListener != null)
onButtonPreparedListener.onPrepared(mButtons);
}
});
}
private void animateStageIn() {
final ValueAnimator animator = new ValueAnimator();
animator.setFloatValues(0F, 1F);
animator.setInterpolator(new FastOutSlowInInterpolator());
animator.setDuration(ANIMATION_DURATION);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float currentAlpha = (float) valueAnimator.getAnimatedValue();
stage.setAlpha(currentAlpha);
}
});
animator.start();
}
private void createBallAnimator(Ball ball, int startDelay) {
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setFloatValues(mMinRadius, mMaxRadius);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.setDuration(mDuration);
valueAnimator.addUpdateListener(new BallUpdateListener(ball));
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.setStartDelay(startDelay);
mBallAnimators.add(valueAnimator);
valueAnimator.start();
}
@NonNull
private ValueAnimator createElevationAnimator(@NonNull ShadowAnimatorImpl impl) {
final ValueAnimator animator = new ValueAnimator();
animator.setInterpolator(ELEVATION_ANIM_INTERPOLATOR);
animator.setDuration(ELEVATION_ANIM_DURATION);
animator.addListener(impl);
animator.addUpdateListener(impl);
animator.setFloatValues(0, 1);
return animator;
}
public TalkingIndicatorView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray style = context.obtainStyledAttributes(attrs, new int[] {
R.attr.arcColor,
R.attr.arcWidth,
R.attr.arcLength,
R.attr.cyclePeriod
});
mArcOffset = 0;
mArcLength = style.getDimensionPixelSize(2, 20);
mArcWidth = style.getDimensionPixelSize(1, 4);
mArcPaint = new Paint();
mArcPaint.setAntiAlias(true);
mArcPaint.setColor(style.getColor(0, 0xFFFF0000));
mArcPaint.setStrokeWidth(mArcWidth);
mArcPaint.setStyle(Paint.Style.STROKE);
ValueAnimator cycleAnimator = new ValueAnimator();
cycleAnimator.setRepeatCount(ValueAnimator.INFINITE);
cycleAnimator.setDuration(style.getInteger(3, 1000));
cycleAnimator.setFloatValues(0, 360);
cycleAnimator.setInterpolator(new LinearInterpolator());
cycleAnimator.addUpdateListener(this);
cycleAnimator.start();
style.recycle();
}
public static ValueAnimator ofFloat(float... values) {
ValueAnimator anim = new ValueAnimator();
anim.setFloatValues(values);
cancelOnDestroyActivity(anim);
return anim;
}
/**
* @param context The Context for this view.
* @param height The LayoutParams for this view.
*/
public ToolbarProgressBarAnimatingView(Context context, LayoutParams layoutParams) {
super(context);
setLayoutParams(layoutParams);
mIsRtl = LocalizationUtils.isLayoutRtl();
mDpToPx = getResources().getDisplayMetrics().density;
mAnimationDrawable = new ColorDrawable(Color.WHITE);
setImageDrawable(mAnimationDrawable);
mListener = new ProgressBarUpdateListener();
mAnimatorSet = new AnimatorSet();
mSlowAnimation = new ValueAnimator();
mSlowAnimation.setFloatValues(0.0f, 1.0f);
mSlowAnimation.addUpdateListener(mListener);
mFastAnimation = new ValueAnimator();
mFastAnimation.setFloatValues(0.0f, 1.0f);
mFastAnimation.addUpdateListener(mListener);
updateAnimationDuration();
mAnimatorSet.playSequentially(mSlowAnimation, mFastAnimation);
AnimatorListener listener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator a) {
// Replay the animation if it has not been canceled.
if (mIsCanceled) return;
// Repeats of the animation should have a start delay.
mAnimatorSet.setStartDelay(ANIMATION_DELAY_MS);
updateAnimationDuration();
// Only restart the animation if the last animation is ending.
if (a == mFastAnimation) mAnimatorSet.start();
}
};
mSlowAnimation.addListener(listener);
mFastAnimation.addListener(listener);
}
/**
* @param context The Context for this view.
* @param height The LayoutParams for this view.
*/
public ToolbarProgressBarAnimatingView(Context context, LayoutParams layoutParams) {
super(context);
setLayoutParams(layoutParams);
mIsRtl = LocalizationUtils.isLayoutRtl();
mDpToPx = getResources().getDisplayMetrics().density;
mAnimationDrawable = new ColorDrawable(Color.WHITE);
setImageDrawable(mAnimationDrawable);
mListener = new ProgressBarUpdateListener();
mAnimatorSet = new AnimatorSet();
mSlowAnimation = new ValueAnimator();
mSlowAnimation.setFloatValues(0.0f, 1.0f);
mSlowAnimation.addUpdateListener(mListener);
mFastAnimation = new ValueAnimator();
mFastAnimation.setFloatValues(0.0f, 1.0f);
mFastAnimation.addUpdateListener(mListener);
updateAnimationDuration();
mAnimatorSet.playSequentially(mSlowAnimation, mFastAnimation);
AnimatorListener listener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator a) {
// Replay the animation if it has not been canceled.
if (mIsCanceled) return;
// Repeats of the animation should have a start delay.
mAnimatorSet.setStartDelay(ANIMATION_DELAY_MS);
updateAnimationDuration();
// Only restart the animation if the last animation is ending.
if (a == mFastAnimation) mAnimatorSet.start();
}
};
mSlowAnimation.addListener(listener);
mFastAnimation.addListener(listener);
}
public static ValueAnimator ofFloat(View target, float... values) {
ValueAnimator anim = new ValueAnimator();
anim.setFloatValues(values);
cancelOnDestroyActivity(anim);
return anim;
}
public static ValueAnimator ofFloat(View target, float... values) {
ValueAnimator anim = new ValueAnimator();
anim.setFloatValues(values);
cancelOnDestroyActivity(anim);
return anim;
}
public void onFlingToDelete(PointF vel) {
final long startTime = AnimationUtils.currentAnimationTimeMillis();
// NOTE: Because it takes time for the first frame of animation to actually be
// called and we expect the animation to be a continuation of the fling, we have
// to account for the time that has elapsed since the fling finished. And since
// we don't have a startDelay, we will always get call to update when we call
// start() (which we want to ignore).
final TimeInterpolator tInterpolator = new TimeInterpolator() {
private int mCount = -1;
private long mStartTime;
private float mOffset;
/* Anonymous inner class ctor */ {
mStartTime = startTime;
}
@Override
public float getInterpolation(float t) {
if (mCount < 0) {
mCount++;
} else if (mCount == 0) {
mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
mCount++;
}
return Math.min(1f, mOffset + t);
}
};
final Rect from = new Rect();
final View dragView = mDragView;
from.left = (int) dragView.getTranslationX();
from.top = (int) dragView.getTranslationY();
AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
from, startTime, FLING_TO_DELETE_FRICTION);
final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
// Create and start the animation
ValueAnimator mDropAnim = new ValueAnimator();
mDropAnim.setInterpolator(tInterpolator);
mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
mDropAnim.setFloatValues(0f, 1f);
mDropAnim.addUpdateListener(updateCb);
mDropAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
onAnimationEndRunnable.run();
}
});
mDropAnim.start();
mDeferringForDelete = true;
}
/**
* @param context The Context for this view.
* @param height The LayoutParams for this view.
*/
public ToolbarProgressBarAnimatingView(Context context, LayoutParams layoutParams) {
super(context);
setLayoutParams(layoutParams);
mIsRtl = LocalizationUtils.isLayoutRtl();
mDpToPx = getResources().getDisplayMetrics().density;
mAnimationDrawable = new ColorDrawable(Color.WHITE);
setImageDrawable(mAnimationDrawable);
mListener = new ProgressBarUpdateListener();
mAnimatorSet = new AnimatorSet();
mSlowAnimation = new ValueAnimator();
mSlowAnimation.setFloatValues(0.0f, 1.0f);
mSlowAnimation.addUpdateListener(mListener);
mFastAnimation = new ValueAnimator();
mFastAnimation.setFloatValues(0.0f, 1.0f);
mFastAnimation.addUpdateListener(mListener);
updateAnimationDuration();
mAnimatorSet.playSequentially(mSlowAnimation, mFastAnimation);
AnimatorListener listener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator a) {
// Replay the animation if it has not been canceled.
if (mIsCanceled) return;
// Repeats of the animation should have a start delay.
mAnimatorSet.setStartDelay(ANIMATION_DELAY_MS);
updateAnimationDuration();
// Only restart the animation if the last animation is ending.
if (a == mFastAnimation) mAnimatorSet.start();
}
};
mSlowAnimation.addListener(listener);
mFastAnimation.addListener(listener);
}
public ProgressBar(Context c, AttributeSet attrs) {
super(c, attrs);
mDensity = c.getResources().getDisplayMetrics().density;
final TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.ProgressBar);
try {
mBarColor = ta.getColor(
R.styleable.ProgressBar_barColor,
c.getResources().getColor(android.R.color.holo_blue_light));
mSolidBarHeight = ta.getDimensionPixelSize(
R.styleable.ProgressBar_barHeight,
Math.round(DEFAULT_BAR_HEIGHT_DP * mDensity));
mSolidBarDetentWidth = ta.getDimensionPixelSize(
R.styleable.ProgressBar_detentWidth,
Math.round(DEFAULT_DETENT_WIDTH_DP * mDensity));
mUseShadow = ta.getBoolean(
R.styleable.ProgressBar_useShadow,
false);
} finally {
ta.recycle();
}
mAnimator = new ValueAnimator();
mAnimator.setFloatValues(1.0f, 2.0f);
mAnimator.setRepeatCount(ValueAnimator.INFINITE);
mAnimator.setInterpolator(new ExponentialInterpolator());
mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
});
mPaint.setColor(mBarColor);
if (mUseShadow) {
mShadow = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{(mBarColor & 0x00ffffff) | 0x22000000, 0});
}
}
public static ValueAnimator ofFloat(View target, float... values) {
ValueAnimator anim = new ValueAnimator();
anim.setFloatValues(values);
cancelOnDestroyActivity(anim);
return anim;
}
public void onFlingToDelete(PointF vel) {
final long startTime = AnimationUtils.currentAnimationTimeMillis();
// NOTE: Because it takes time for the first frame of animation to actually be
// called and we expect the animation to be a continuation of the fling, we have
// to account for the time that has elapsed since the fling finished. And since
// we don't have a startDelay, we will always get call to update when we call
// start() (which we want to ignore).
final TimeInterpolator tInterpolator = new TimeInterpolator() {
private int mCount = -1;
private long mStartTime;
private float mOffset;
/* Anonymous inner class ctor */ {
mStartTime = startTime;
}
@Override
public float getInterpolation(float t) {
if (mCount < 0) {
mCount++;
} else if (mCount == 0) {
mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
mCount++;
}
return Math.min(1f, mOffset + t);
}
};
final Rect from = new Rect();
final View dragView = mDragView;
from.left = (int) dragView.getTranslationX();
from.top = (int) dragView.getTranslationY();
AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
from, startTime, FLING_TO_DELETE_FRICTION);
final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
// Create and start the animation
ValueAnimator mDropAnim = new ValueAnimator();
mDropAnim.setInterpolator(tInterpolator);
mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
mDropAnim.setFloatValues(0f, 1f);
mDropAnim.addUpdateListener(updateCb);
mDropAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
onAnimationEndRunnable.run();
}
});
mDropAnim.start();
mDeferringForDelete = true;
}