下面列出了怎么用android.view.ViewAnimationUtils的API类实例代码及写法,或者点击链接到github查看源代码。
public static void circleAnimate(final View view, int cx, int cy) {
if (view == null) return;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setVisibility(View.INVISIBLE);
int finalRadius = Math.max(view.getWidth(), view.getHeight());
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
anim.setDuration(500);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
view.setVisibility(View.VISIBLE);
}
});
anim.start();
}
} catch (IllegalStateException e) {
view.setVisibility(View.VISIBLE);
}
}
@MainThread
private void hide() {
if (getVisibility() == View.VISIBLE) {
if (listener != null) listener.onSearchClosed();
if (Build.VERSION.SDK_INT >= 21) {
Animator animator = ViewAnimationUtils.createCircularReveal(this, (int)x, (int)y, getWidth(), 0);
animator.setDuration(400);
animator.addListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
setVisibility(View.INVISIBLE);
}
});
animator.start();
} else {
setVisibility(View.INVISIBLE);
}
}
}
@MainThread
public void display(float x, float y) {
if (getVisibility() != View.VISIBLE) {
this.x = x;
this.y = y;
searchItem.expandActionView();
if (Build.VERSION.SDK_INT >= 21) {
Animator animator = ViewAnimationUtils.createCircularReveal(this, (int)x, (int)y, 0, getWidth());
animator.setDuration(400);
setVisibility(View.VISIBLE);
animator.start();
} else {
setVisibility(View.VISIBLE);
}
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void startCircularRevealExitAnimation(Context context, final View view, RevealAnimationSetting revealSettings, int startColor, int endColor, final AnimationFinishedListener listener) {
if (isAnimationEnabled() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cx = revealSettings.getCenterX();
int cy = revealSettings.getCenterY();
int width = revealSettings.getWidth();
int height = revealSettings.getHeight();
float initRadius = (float) Math.sqrt(width * width + height * height);
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, initRadius, 0);
anim.setDuration(getMediumDuration(context));
anim.setInterpolator(new FastOutSlowInInterpolator());
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
//Important: This will prevent the view's flashing (visible between the finished animation and the Fragment remove)
view.setVisibility(View.GONE);
listener.onAnimationFinished();
}
});
anim.start();
startBackgroundColorAnimation(view, startColor, endColor, getMediumDuration(context));
} else {
listener.onAnimationFinished();
}
}
/**
* 向四周伸张,直到完成显示。
*/
@SuppressLint("NewApi")
public static void show(View myView, float startRadius, long durationMills) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
myView.setVisibility(View.VISIBLE);
return;
}
int cx = (myView.getLeft() + myView.getRight()) / 2;
int cy = (myView.getTop() + myView.getBottom()) / 2;
int w = myView.getWidth();
int h = myView.getHeight();
// 勾股定理 & 进一法
int finalRadius = (int) Math.sqrt(w * w + h * h) + 1;
Animator anim =
ViewAnimationUtils.createCircularReveal(myView, cx, cy, startRadius, finalRadius);
myView.setVisibility(View.VISIBLE);
anim.setDuration(durationMills);
anim.start();
}
private void circularRevealActivity() {
int cx = background.getRight() - getDips(44);
int cy = background.getBottom() - getDips(44);
float finalRadius = Math.max(background.getWidth(), background.getHeight());
Animator circularReveal = ViewAnimationUtils.createCircularReveal(
background,
cx,
cy,
0,
finalRadius);
circularReveal.setDuration(3000);
background.setVisibility(View.VISIBLE);
circularReveal.start();
}
private void animateAppAndStatusBar(int cx, final int toColor) {
Animator animator = ViewAnimationUtils.createCircularReveal(
mRevealView,
cx,
appBarLayout.getBottom(), 0,
appBarLayout.getWidth() / 2);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
mRevealView.setBackgroundColor(getResources().getColor(toColor));
}
});
mRevealBackgroundView.setBackgroundColor(getResources().getColor(fromColor));
animator.setStartDelay(200);
animator.setDuration(125);
animator.start();
mRevealView.setVisibility(View.VISIBLE);
fromColor = toColor;
}
@MainThread
private void hide() {
if (getVisibility() == View.VISIBLE) {
if (listener != null) listener.onSearchClosed();
if (Build.VERSION.SDK_INT >= 21) {
Animator animator = ViewAnimationUtils.createCircularReveal(this, (int)x, (int)y, getWidth(), 0);
animator.setDuration(400);
animator.addListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
setVisibility(View.INVISIBLE);
}
});
animator.start();
} else {
setVisibility(View.INVISIBLE);
}
}
}
private void animateAvatarSelectorHide(int duration) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// get the center for the clipping circle
int cx = (int) fab.getX() + fab.getWidth() / 2;
int cy = 0;
// get the final radius for the clipping circle
int finalRadius = (int) Math.hypot(userAvatarBg.getWidth(), bg.getHeight() - userAvatarBg.getHeight());
// create the animator for this view (the start radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(gridContainer, cx, cy, finalRadius, 0);
// make the view visible and start the animation
anim.setInterpolator(new AccelerateInterpolator());
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
gridContainer.setVisibility(View.GONE);
setSwitchFab();
super.onAnimationEnd(animation);
}
});
anim.setDuration(duration).start();
}
}
private void animateAvatarBg(int duration, int x, Animator.AnimatorListener cb) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
userAvatarBg.setVisibility(View.INVISIBLE);
// get the center for the clipping circle
int cx = (userAvatarBg.getLeft() + userAvatarBg.getRight()) / 2;
int cy = userAvatarBg.getBottom();
// get the final radius for the clipping circle
int finalRadius = (int) Math.hypot(userAvatarBg.getWidth(), userAvatarBg.getHeight());
// create the animator for this view (the start radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(userAvatarBg, x, cy, ScreenUtil.dpToPx(getResources(), 100f), finalRadius);
// make the view visible and start the animation
userAvatarBg.setVisibility(View.VISIBLE);
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(duration);
if (cb != null) {
anim.addListener(cb);
}
anim.start();
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void listing12_3() {
// Listing 12-3: Using a circular reveal to show a View
final View view = findViewById(R.id.hidden_view);
// Center the reveal on the middle of the View
int centerX = view.getWidth() / 2;
int centerY = view.getHeight() / 2;
// Determine what radius circle will cover the entire View
float coveringRadius = (float) Math.hypot(centerX, centerY);
// Build the circular reveal
Animator anim = ViewAnimationUtils.createCircularReveal(
view,
centerX,
centerY,
0, // initial radius
coveringRadius // final covering radius
);
// Set the View to VISIBLE before starting the animation
view.setVisibility(View.VISIBLE);
anim.start();
}
private void showVideoStartTip() {
mRlVideoTip.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Animator circularReveal = ViewAnimationUtils.createCircularReveal(mRlVideoTip,
mIvCover.getWidth() - ArmsUtils.dip2px(VideoDetailActivity.this, mAnchorX),
mIvCover.getHeight() - ArmsUtils.dip2px(VideoDetailActivity.this, mAnchorY),
0,
((float) Math.hypot(mIvCover.getWidth(), mIvCover.getHeight())));
circularReveal.setDuration(800);
circularReveal.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mIvCover.setVisibility(View.GONE);
mPresenter.loadPlayUrl(aid);
}
});
circularReveal.start();
} else {
mPresenter.loadPlayUrl(aid);
}
// 锁定AppBarLayout
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) mAppbar.getChildAt(0).getLayoutParams();
layoutParams.setScrollFlags(0);
mAppbar.getChildAt(0).setLayoutParams(layoutParams);
}
@UiThread public static void revealPopupWindow(@NonNull PopupWindow popupWindow, @NonNull View from) {
Rect rect = ViewHelper.getLayoutPosition(from);
int x = (int) rect.exactCenterX();
int y = (int) rect.exactCenterY();
if (popupWindow.getContentView() != null) {
View view = popupWindow.getContentView();
if (view != null) {
popupWindow.showAsDropDown(from);
view.post(() -> {
if (ViewCompat.isAttachedToWindow(view)) {
Animator animator = ViewAnimationUtils.createCircularReveal(view, x, y, 0,
(float) Math.hypot(rect.width(), rect.height()));
animator.setDuration(view.getResources().getInteger(android.R.integer.config_shortAnimTime));
animator.start();
}
});
}
}
}
@UiThread public static void revealDialog(@NonNull Dialog dialog, int animDuration) {
if (dialog.getWindow() != null) {
View view = dialog.getWindow().getDecorView();
if (view != null) {
view.post(() -> {
if (ViewCompat.isAttachedToWindow(view)) {
int centerX = view.getWidth() / 2;
int centerY = view.getHeight() / 2;
Animator animator = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 20, view.getHeight());
animator.setDuration(animDuration);
animator.start();
}
});
}
}
}
@UiThread public static void dismissDialog(@NonNull DialogFragment dialogFragment, int duration, AnimatorListenerAdapter listenerAdapter) {
Dialog dialog = dialogFragment.getDialog();
if (dialog != null) {
if (dialog.getWindow() != null) {
View view = dialog.getWindow().getDecorView();
if (view != null) {
int centerX = view.getWidth() / 2;
int centerY = view.getHeight() / 2;
float radius = (float) Math.sqrt(view.getWidth() * view.getWidth() / 4 + view.getHeight() * view.getHeight() / 4);
view.post(() -> {
if (ViewCompat.isAttachedToWindow(view)) {
Animator animator = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, radius, 0);
animator.setDuration(duration);
animator.addListener(listenerAdapter);
animator.start();
} else {
listenerAdapter.onAnimationEnd(null);
}
});
}
}
} else {
listenerAdapter.onAnimationEnd(null);
}
}
@Override
public void animate(
View from, View to, NavigationType navType, Direction direction, final Callback callback) {
int[] clickedViewCenter = getCenterClickedView((ViewGroup) from);
int circularRevealCenterX = clickedViewCenter[0];
int circularRevealCenterY = clickedViewCenter[1];
float finalRadius = (float) Math.hypot(to.getWidth(), to.getHeight());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Animator anim =
ViewAnimationUtils.createCircularReveal(to, circularRevealCenterX,
circularRevealCenterY, 0, finalRadius);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
callback.onAnimationEnd();
}
});
anim.start();
} else {
callback.onAnimationEnd();
}
}
@Override
public void showTitle(String title) {
playlistTitle.setText(title);
playlistTitle.setScaleX(0);
playlistTitle.setScaleY(0);
titleBackground.post(() -> {
int cx = titleBackground.getWidth() / 2;
int cy = titleBackground.getHeight() / 2;
Animator animator = ViewAnimationUtils.createCircularReveal(titleBackground, cx, cy, 0,
(int) Math.hypot(titleBackground.getWidth(), titleBackground.getHeight()));
animator.setDuration(400);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
titleBackground.setVisibility(View.VISIBLE);
playlistTitle.animate()
.setDuration(400)
.scaleX(1).scaleY(1)
.setInterpolator(new OvershootInterpolator())
.start();
}
});
animator.start();
});
}
private void performRevealAnimation(final View view, int screenCenterX, int screenCenterY) {
int[] animatingViewCoords = new int[2];
view.getLocationOnScreen(animatingViewCoords);
int centerX = screenCenterX - animatingViewCoords[0];
int centerY = screenCenterY - animatingViewCoords[1];
Point size = new Point();
getActivity().getWindowManager().getDefaultDisplay().getSize(size);
int maxRadius = size.y;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setVisibility(View.VISIBLE);
Animator animator = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 0, maxRadius);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.INVISIBLE);
}
});
animator.start();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void prepareCircularReveal(View startView, FrameLayout targetView) {
int centerX = (startView.getLeft() + startView.getRight()) / 2;
// Subtract the start view's height to adjust for relative coordinates on screen.
int centerY = (startView.getTop() + startView.getBottom()) / 2 - startView.getHeight();
float endRadius = (float) Math.hypot(centerX, centerY);
mCircularReveal = ViewAnimationUtils.createCircularReveal(
targetView, centerX, centerY, startView.getWidth(), endRadius);
mCircularReveal.setInterpolator(new FastOutLinearInInterpolator());
mCircularReveal.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mIcon.setVisibility(View.GONE);
mCircularReveal.removeListener(this);
}
});
// Adding a color animation from the FAB's color to transparent creates a dissolve like
// effect to the circular reveal.
int accentColor = ContextCompat.getColor(this, mCategory.getTheme().getAccentColor());
mColorChange = ObjectAnimator.ofInt(targetView,
ViewUtils.FOREGROUND_COLOR, accentColor, Color.TRANSPARENT);
mColorChange.setEvaluator(new ArgbEvaluator());
mColorChange.setInterpolator(mInterpolator);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void hideWithReveal(View view, Point point) {
if(DeviceUtils.supportV5()) {
// get the initial radius for the clipping circle
int initialRadius = (int) Math.hypot(view.getWidth(), view.getHeight());
// create the animation (the final radius is zero)
Animator animator = ViewAnimationUtils.createCircularReveal(view, point.x, point.y, initialRadius, 0);
// make the view invisible when the animation is done
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.INVISIBLE);
}
});
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.setDuration(500);
animator.start();
} else {
view.setVisibility(View.INVISIBLE);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void circularRevealActivity(View v1, View v2) {
int cx = v1.getWidth() / 2;
int cy = v1.getHeight() / 2;
float finalRadius = Math.max(v1.getWidth(), v1.getHeight());
// create the animator for this view (the start radius is zero)
Animator circularReveal = ViewAnimationUtils.createCircularReveal(v1, cx, cy, 0, finalRadius);
circularReveal.setDuration(1000);
// make the view visible and start the animation
v2.setVisibility(View.INVISIBLE);
v1.setVisibility(View.VISIBLE);
circularReveal.start();
}
private void showRevealEditText(FrameLayout view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cx = view.getRight() - 30;
int cy = view.getBottom() - 60;
int finalRadius = Math.max(view.getWidth(), view.getHeight());
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
view.setVisibility(View.VISIBLE);
isEditTextVisible = true;
anim.start();
} else {
view.setVisibility(View.VISIBLE);
isEditTextVisible = true;
}
}
private void hideReavelEditText(final FrameLayout view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cx = view.getRight() - 30;
int cy = view.getBottom() - 60;
int initialRadius = view.getWidth();
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, initialRadius, 0);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.INVISIBLE);
}
});
isEditTextVisible = false;
anim.start();
} else {
view.setVisibility(View.INVISIBLE);
isEditTextVisible = false;
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void contractLollipop(int x, int y, float startRadius, float endRadius) {
Animator toolbarContractAnim = ViewAnimationUtils.createCircularReveal(
mFabExpandLayout, x, y, startRadius, endRadius);
toolbarContractAnim.setDuration(animationDuration);
toolbarContractAnim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
contractAnimationEnd();
}
});
toolbarContractAnim.start();
}
public static void reveal(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// get the center for the clipping circle
int cx = view.getWidth() / 2;
int cy = view.getHeight() / 2;
// get the final radius for the clipping circle
float finalRadius = (float) Math.hypot(cx, cy);
// create the animator for this view (the start radius is zero)
Animator anim =
ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
// make the view visible and start the animation
view.setVisibility(View.VISIBLE);
anim.start();
} else {
view.setVisibility(View.VISIBLE);
view.animate().alpha(1f).start();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void revealInAnimation(final Context mContext, final View view, int duration) {
int cx = view.getWidth() - mContext.getResources().getDimensionPixelSize(R.dimen.reveal);
int cy = view.getHeight() / 2;
if (cx != 0 && cy != 0) {
float finalRadius = (float) Math.hypot(cx, cy);
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0.0f, finalRadius);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(duration);
view.setVisibility(View.VISIBLE);
anim.start();
}
}
/**
* 向四周伸张,直到完成显示。
*/
@SuppressLint("NewApi")
public static void show(View myView, float startRadius, long durationMills) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
myView.setVisibility(View.VISIBLE);
return;
}
int cx = (myView.getLeft() + myView.getRight()) / 2;
int cy = (myView.getTop() + myView.getBottom()) / 2;
int w = myView.getWidth();
int h = myView.getHeight();
// 勾股定理 & 进一法
int finalRadius = (int) Math.sqrt(w * w + h * h) + 1;
Animator anim =
ViewAnimationUtils.createCircularReveal(myView, cx, cy, startRadius, finalRadius);
myView.setVisibility(View.VISIBLE);
anim.setDuration(durationMills);
anim.start();
}
public static void circleAnimate(final View view, int cx, int cy) {
if (view == null) return;
try {
view.setVisibility(View.INVISIBLE);
int finalRadius = Math.max(view.getWidth(), view.getHeight());
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
anim.setDuration(500);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
view.setVisibility(View.VISIBLE);
}
});
anim.start();
} catch (IllegalStateException e) {
view.setVisibility(View.VISIBLE);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void circularReveal(@NonNull final View anchor) {
final View contentView = getContentView();
contentView.post(new Runnable() {
@Override
public void run() {
final int[] myLocation = new int[2];
final int[] anchorLocation = new int[2];
contentView.getLocationOnScreen(myLocation);
anchor.getLocationOnScreen(anchorLocation);
final int cx = anchorLocation[0] - myLocation[0] + anchor.getWidth()/2;
final int cy = anchorLocation[1] - myLocation[1] + anchor.getHeight()/2;
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
final int dx = Math.max(cx, contentView.getMeasuredWidth() - cx);
final int dy = Math.max(cy, contentView.getMeasuredHeight() - cy);
final float finalRadius = (float) Math.hypot(dx, dy);
Animator animator = ViewAnimationUtils.createCircularReveal(contentView, cx, cy, 0f, finalRadius);
animator.setDuration(500);
animator.start();
}
});
}
/**
* Returns an Animator to animate a clipping circle.
*
* <p>This is meant to be used as a drop-in replacement for {@link
* ViewAnimationUtils#createCircularReveal(View, int, int, float, float)}. In pre-L APIs, a
* backwards compatible version of the Animator will be returned.
*
* <p>You must also call {@link
* CircularRevealCompat#createCircularRevealListener(CircularRevealWidget)} and add the returned
* AnimatorListener to this Animator or preferably to the overall AnimatorSet.
*/
@NonNull
public static Animator createCircularReveal(
CircularRevealWidget view, float centerX, float centerY, float startRadius, float endRadius) {
Animator revealInfoAnimator =
ObjectAnimator.ofObject(
view,
CircularRevealProperty.CIRCULAR_REVEAL,
CircularRevealEvaluator.CIRCULAR_REVEAL,
new RevealInfo(centerX, centerY, startRadius),
new RevealInfo(centerX, centerY, endRadius));
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
Animator circularRevealAnimator =
ViewAnimationUtils.createCircularReveal(
(View) view, (int) centerX, (int) centerY, startRadius, endRadius);
AnimatorSet set = new AnimatorSet();
set.playTogether(revealInfoAnimator, circularRevealAnimator);
return set;
} else {
return revealInfoAnimator;
}
}