下面列出了怎么用android.view.ViewPropertyAnimator的API类实例代码及写法,或者点击链接到github查看源代码。
private void setupView() {
setContentView(R.layout.scaledview_activity);
View scaledView = findViewById(R.id.scaled_view);
scaledView.setOnClickListener(
new View.OnClickListener() {
private boolean scaled = false;
@Override
public void onClick(View v) {
ViewPropertyAnimator animator = v.animate();
animator.cancel();
if (scaled) {
animator.scaleX(1f).scaleY(1f).start();
scaled = !scaled;
} else {
animator.scaleX(0.5f).scaleY(0.5f).start();
scaled = !scaled;
}
}
});
}
private void removeFixViewWithAnimator(RecyclerView.Recycler recycler,
LayoutManagerHelper layoutManagerHelper, View fixView) {
if (!isRemoveFixViewImmediately && mFixViewAnimatorHelper != null) {
ViewPropertyAnimator animator = mFixViewAnimatorHelper
.onGetFixViewDisappearAnimator(fixView);
if (animator != null) {
mFixViewDisappearAnimatorListener
.bindAction(recycler, layoutManagerHelper, fixView);
animator.setListener(mFixViewDisappearAnimatorListener).start();
isAddFixViewImmediately = false;
} else {
layoutManagerHelper.removeChildView(fixView);
recycler.recycleView(fixView);
isAddFixViewImmediately = false;
}
} else {
layoutManagerHelper.removeChildView(fixView);
recycler.recycleView(fixView);
isAddFixViewImmediately = false;
}
}
/**
* Adapts the visibility of the view, which is shown, when the tab switcher is empty.
*
* @param animationDuration
* The duration of the fade animation, which should be used to show or hide the view, in
* milliseconds as a {@link Long} value
*/
private void adaptEmptyView(final long animationDuration) {
detachEmptyView();
if (getModel().isEmpty()) {
emptyView = getModel().getEmptyView();
if (emptyView != null) {
emptyView.setAlpha(0);
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(emptyView.getLayoutParams().width,
emptyView.getLayoutParams().height);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
getTabSwitcher().addView(emptyView, 0, layoutParams);
ViewPropertyAnimator animation = emptyView.animate();
animation.setDuration(
animationDuration == -1 ? emptyViewAnimationDuration : animationDuration);
animation.alpha(1);
animation.start();
}
}
}
public static ViewPropertyAnimator fadeOut(final View from, int duration) {
if (from.getVisibility() == View.VISIBLE) {
from.clearAnimation();
final ViewPropertyAnimator animator = from.animate();
animator.alpha(0f)
.setDuration(duration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
from.setVisibility(View.INVISIBLE);
from.setAlpha(1f);
}
});
return animator;
}
return null;
}
public static void createScaleInScaleOutAnim(final View view, final Runnable action) {
final int animTime = Setup.appSettings().getAnimationSpeed() * 4;
ViewPropertyAnimator animateScaleIn = view.animate().scaleX(0.85f).scaleY(0.85f).setDuration(animTime);
animateScaleIn.setInterpolator(new AccelerateDecelerateInterpolator());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ViewPropertyAnimator animateScaleOut = view.animate().scaleX(1.0f).scaleY(1.0f).setDuration(animTime);
animateScaleOut.setInterpolator(new AccelerateDecelerateInterpolator());
new Handler().postDelayed(new Runnable() {
public final void run() {
action.run();
}
}, animTime);
}
}, animTime);
}
public long getStartDelay()
{
ViewPropertyAnimator viewpropertyanimator = (ViewPropertyAnimator)b.get();
if (viewpropertyanimator != null)
{
return viewpropertyanimator.getStartDelay();
} else
{
return -1L;
}
}
public com.nineoldandroids.view.ViewPropertyAnimator translationYBy(float f)
{
ViewPropertyAnimator viewpropertyanimator = (ViewPropertyAnimator)b.get();
if (viewpropertyanimator != null)
{
viewpropertyanimator.translationYBy(f);
}
return this;
}
/**
* Reduces the X & Y
*
* @param v the view to be scaled
*
* @return the ViewPropertyAnimation to manage the animation
*/
public static ViewPropertyAnimator hideViewByScale (View v) {
ViewPropertyAnimator propertyAnimator = v.animate().setStartDelay(DEFAULT_DELAY).setDuration(SHORT_DURATION)
.scaleX(0).scaleY(0);
return propertyAnimator;
}
/**
* Shows a view by scaling
*
* @param v the view to be scaled
*
* @return the ViewPropertyAnimation to manage the animation
*/
public static ViewPropertyAnimator showViewByScale (View v) {
ViewPropertyAnimator propertyAnimator = v.animate().setStartDelay(DEFAULT_DELAY)
.scaleX(1).scaleY(1);
return propertyAnimator;
}
@CheckResult
@NonNull
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public static Observable<Animator> starts(ViewPropertyAnimator animator) {
checkNotNull(animator, "animator == null");
return Observable.create(new ViewPropertyAnimatorListenerOnSubscribe(animator, AnimationEvent.START));
}
public static ViewPropertyAnimator showViewByScaleAndVisibility (View v) {
v.setVisibility(View.VISIBLE);
ViewPropertyAnimator propertyAnimator = v.animate().setStartDelay(DEFAULT_DELAY)
.scaleX(1).scaleY(1);
return propertyAnimator;
}
public static ViewPropertyAnimator showScrollbar(View mScrollbar, Context context) {
float transX = context.getResources().getDimensionPixelSize(R.dimen.fastscroll_bubble_size);
mScrollbar.setTranslationX(transX);
mScrollbar.setVisibility(View.VISIBLE);
return mScrollbar.animate().translationX(0f).alpha(1f)
.setDuration(Constants.sScrollbarAnimDuration)
.setListener(new AnimatorListenerAdapter() {
// adapter required for new alpha value to stick
});
}
public static ViewPropertyAnimator fadeViewIn(View view)
{
view.setVisibility(View.VISIBLE);
return view.animate()
.withLayer()
.setDuration(100)
.alpha(1.0f);
}
public static ViewPropertyAnimator fadeViewOut(View view)
{
return view.animate()
.withLayer()
.setDuration(300)
.alpha(0.0f);
}
public void applyPreview(Bitmap bitmap, boolean animate) {
if (bitmap != null) {
mWidgetImage.setBitmap(bitmap,
DrawableFactory.get(getContext()).getBadgeForUser(mItem.user, getContext()));
if (mAnimatePreview) {
mWidgetImage.setAlpha(0f);
ViewPropertyAnimator anim = mWidgetImage.animate();
anim.alpha(1.0f).setDuration(FADE_IN_DURATION_MS);
} else {
mWidgetImage.setAlpha(1f);
}
}
}
private void animateOut() {
text1.setVisibility(View.GONE);
container2.setVisibility(View.VISIBLE);
container2.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
container2.getViewTreeObserver().removeOnPreDrawListener(this);
ViewPropertyAnimator animator = animateViewsOut();
animator.setListener(new SimpleAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
container2.setVisibility(View.GONE);
animateAlpha();
}
});
animator.start();
container3.animate().alpha(1f).setDuration(300).setStartDelay(600).start();
ObjectAnimator background = ObjectAnimator.ofObject(container, "backgroundColor",
new ArgbEvaluator(), yellow,
green);
background.setDuration(300);
background.setStartDelay(600);
background.start();
return false;
}
});
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void animateClose() {
mBalloonMarkerDrawable.stop();
ViewPropertyAnimator animator = mNumber.animate();
animator.alpha(0f);
animator.setDuration(100);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
animator.withEndAction(new Runnable() {
@Override
public void run() {
//We use INVISIBLE instead of GONE to avoid a requestLayout
mNumber.setVisibility(View.INVISIBLE);
mBalloonMarkerDrawable.animateToNormal();
}
});
} else {
animator.setListener(new AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
//We use INVISIBLE instead of GONE to avoid a requestLayout
mNumber.setVisibility(View.INVISIBLE);
mBalloonMarkerDrawable.animateToNormal();
}
});
}
animator.start();
}
private ViewPropertyAnimator animateButton(final View button, final int direction) {
final float outerX = getWidth();
final float innerX = button.getX() - button.getTranslationX();
mInterfaceState.removeFromCache((View)getParent());
if (ANIMATION_IN == direction) {
button.setClickable(true);
return button.animate().translationX(0);
}
button.setClickable(false);
return button.animate().translationX(outerX - innerX);
}
public static void applyAlpha(View view, long delay, float alpha, Animator.AnimatorListener animatorListener){
ViewPropertyAnimator animator2 = view.animate();
animator2.alpha(alpha);
animator2.setInterpolator(new DecelerateInterpolator());
animator2.setStartDelay(delay);
animator2.setListener(animatorListener);
animator2.start();
}
@Override
public final void animatePosition(@NonNull final Axis axis,
@NonNull final ViewPropertyAnimator animator,
@NonNull final AbstractItem item, final float position,
final boolean includePadding) {
throw new UnsupportedOperationException();
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set.");
// Get element from your dataset at this position and replace the contents of the view
// with that element
Log.e("jus", jarr.get(position).asJsonObject().getString("pic"));
viewHolder.niv.setImageUrl(jarr.get(position).asJsonObject().getString("pic"), imageLoader);
/// COOL ANIM
View v = (View) viewHolder.niv.getParent().getParent();
if (v != null && !positionsMapper.get(position) && position > lastPosition) {
speed = scrollListener.getSpeed();
animDuration = (((int) speed) == 0) ? ANIM_DEFAULT_SPEED : (long) ((1 / speed) * 3000);
Log.d(TAG, "scroll speed/dur : " + speed + " / " + animDuration);
//animDuration = ANIM_DEFAULT_SPEED;
if (animDuration > ANIM_DEFAULT_SPEED)
animDuration = ANIM_DEFAULT_SPEED;
lastPosition = position;
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(35.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
ViewPropertyAnimator localViewPropertyAnimator =
v.animate().rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0)
.setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
localViewPropertyAnimator.setStartDelay(0).start();
positionsMapper.put(position, true);
}
}
@Override
public void setupAnimation(View item, int position, int scrollDirection, ViewPropertyAnimator animator) {
animator
.translationYBy(-item.getHeight() / 2 * scrollDirection)
.scaleX(1)
.scaleY(1)
.alpha(JazzyHelper.OPAQUE);
}
public long getDuration()
{
ViewPropertyAnimator viewpropertyanimator = (ViewPropertyAnimator)b.get();
if (viewpropertyanimator != null)
{
return viewpropertyanimator.getDuration();
} else
{
return -1L;
}
}
@Override
public final void animateScale(@NonNull final Axis axis,
@NonNull final ViewPropertyAnimator animator,
final float scale) {
Condition.INSTANCE.ensureNotNull(axis, "The axis may not be null");
Condition.INSTANCE.ensureNotNull(animator, "The animator may not be null");
if (getOrientationInvariantAxis(axis) == Axis.DRAGGING_AXIS) {
animator.scaleY(scale);
} else {
animator.scaleX(scale);
}
}
private void rotate(final View v, boolean reverse) {
ViewPropertyAnimator animator = v.animate().setDuration(500).rotation(reverse ? -360 : 360);
animator.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
v.setRotation(0);
}
});
animator.start();
}
/**
* Animates the position and size of a specific tab in order to hide the tab switcher.
*
* @param item
* The item, which corresponds to the tab, which should be animated, as an instance of
* the class {@link AbstractItem}. The item may not be null
* @param duration
* The duration of the animation in milliseconds as a {@link Long} value
* @param interpolator
* The interpolator, which should be used by the animation, as an instance of the class
* {@link Interpolator}. The interpolator may not be null
* @param delay
* The delay of the animation in milliseconds as a {@link Long} value
* @param listener
* The listener, which should be notified about the animation's progress, as an instance
* of the type {@link AnimatorListener} or null, if no listener should be notified
*/
private void animateHideSwitcher(@NonNull final AbstractItem item, final long duration,
@NonNull final Interpolator interpolator, final long delay,
@Nullable final AnimatorListener listener) {
View view = item.getView();
animateBottomMargin(view, -(tabInset + tabBorderWidth), duration, delay);
ViewPropertyAnimator animation = view.animate();
animation.setDuration(duration);
animation.setInterpolator(interpolator);
animation.setListener(new AnimationListenerWrapper(listener));
getArithmetics().animateScale(Axis.DRAGGING_AXIS, animation, 1);
getArithmetics().animateScale(Axis.ORTHOGONAL_AXIS, animation, 1);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
getArithmetics().animatePosition(Axis.ORTHOGONAL_AXIS, animation, item,
getTabSwitcher().getLayout() == Layout.PHONE_LANDSCAPE ? layoutParams.topMargin :
0);
int selectedTabIndex = getModel().getSelectedTabIndex();
if (item.getIndex() < selectedTabIndex) {
getArithmetics().animatePosition(Axis.DRAGGING_AXIS, animation, item,
getArithmetics().getTabContainerSize(Axis.DRAGGING_AXIS));
} else if (item.getIndex() > selectedTabIndex) {
getArithmetics().animatePosition(Axis.DRAGGING_AXIS, animation, item,
getTabSwitcher().getLayout() == Layout.PHONE_LANDSCAPE ? 0 :
layoutParams.topMargin);
} else {
getArithmetics().animatePosition(Axis.DRAGGING_AXIS, animation, item,
getTabSwitcher().getLayout() == Layout.PHONE_LANDSCAPE ? 0 :
layoutParams.topMargin);
}
animation.setStartDelay(delay);
animation.start();
}
/**
* Animates to rotation of all tabs to be reset to normal.
*
* @param interpolator
* The interpolator, which should be used by the animation, as an instance of the type
* {@link Interpolator}. The interpolator may not be null
* @param maxAngle
* The angle, the tabs may be rotated by at maximum, in degrees as a {@link Float}
* value
* @param listener
* The listener, which should be notified about the animation's progress, as an instance
* of the type {@link AnimatorListener} or null, if no listener should be notified
* @return True, if at least one tab was animated, false otherwise
*/
private boolean animateTilt(@NonNull final Interpolator interpolator, final float maxAngle,
@Nullable final AnimatorListener listener) {
ItemIterator iterator =
new ItemIterator.Builder(getTabSwitcher(), tabViewRecycler).reverse(true).create();
AbstractItem item;
boolean result = false;
while ((item = iterator.next()) != null) {
if (item.isInflated() &&
getArithmetics().getRotation(Axis.ORTHOGONAL_AXIS, item) != 0) {
View view = item.getView();
ViewPropertyAnimator animation = view.animate();
animation.setListener(new AnimationListenerWrapper(
createRevertOvershootAnimationListener(item, !result ? listener : null)));
animation.setDuration(Math.round(revertOvershootAnimationDuration *
(Math.abs(getArithmetics().getRotation(Axis.ORTHOGONAL_AXIS, item)) /
maxAngle)));
animation.setInterpolator(interpolator);
getArithmetics().animateRotation(Axis.ORTHOGONAL_AXIS, animation, 0);
animation.setStartDelay(0);
animation.start();
result = true;
}
}
return result;
}
public com.nineoldandroids.view.ViewPropertyAnimator setInterpolator(Interpolator interpolator)
{
ViewPropertyAnimator viewpropertyanimator = (ViewPropertyAnimator)b.get();
if (viewpropertyanimator != null)
{
viewpropertyanimator.setInterpolator(interpolator);
}
return this;
}
/**
* Starts a peek animation to add a specific tab.
*
* @param item
* The item, which corresponds to the tab, which should be added, as an instance of the
* class {@link AbstractItem}. The item may not be null
* @param duration
* The duration of the animation in milliseconds as a {@link Long} value
* @param interpolator
* The interpolator, which should be used by the animation, as an instance of the type
* {@link Interpolator}. The interpolator may not be null
* @param peekPosition
* The position on the dragging axis, the tab should be moved to, in pixels as a {@link
* Float} value
* @param peekAnimation
* The peek animation, which has been used to add the tab, as an instance of the class
* {@link PeekAnimation}. The peek animation may not be null
*/
private void animatePeek(@NonNull final AbstractItem item, final long duration,
@NonNull final Interpolator interpolator, final float peekPosition,
@NonNull final PeekAnimation peekAnimation) {
PhoneTabViewHolder viewHolder = (PhoneTabViewHolder) ((TabItem) item).getViewHolder();
viewHolder.closeButton.setVisibility(View.GONE);
View view = item.getView();
float x = peekAnimation.getX();
float y = peekAnimation.getY() + tabTitleContainerHeight;
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
view.setAlpha(1f);
getArithmetics().setPivot(Axis.X_AXIS, item, x);
getArithmetics().setPivot(Axis.Y_AXIS, item, y);
view.setX(layoutParams.leftMargin);
view.setY(layoutParams.topMargin);
getArithmetics().setScale(Axis.DRAGGING_AXIS, item, 0);
getArithmetics().setScale(Axis.ORTHOGONAL_AXIS, item, 0);
ViewPropertyAnimator animation = view.animate();
animation.setInterpolator(interpolator);
animation.setListener(
new AnimationListenerWrapper(createPeekAnimationListener(item, peekAnimation)));
animation.setStartDelay(0);
animation.setDuration(duration);
getArithmetics().animateScale(Axis.DRAGGING_AXIS, animation, 1);
getArithmetics().animateScale(Axis.ORTHOGONAL_AXIS, animation, 1);
getArithmetics().animatePosition(Axis.DRAGGING_AXIS, animation, item, peekPosition, true);
animation.start();
int selectedTabIndex = getModel().getSelectedTabIndex();
TabItem selectedItem = TabItem.create(getModel(), tabViewRecycler, selectedTabIndex);
tabViewRecycler.inflate(selectedItem);
selectedItem.getTag().setPosition(0);
PhoneTabViewHolder selectedTabViewHolder =
(PhoneTabViewHolder) selectedItem.getViewHolder();
selectedTabViewHolder.closeButton.setVisibility(View.GONE);
animateShowSwitcher(selectedItem, duration, interpolator,
createZoomOutAnimationListener(selectedItem, peekAnimation));
}
public com.nineoldandroids.view.ViewPropertyAnimator x(float f)
{
ViewPropertyAnimator viewpropertyanimator = (ViewPropertyAnimator)b.get();
if (viewpropertyanimator != null)
{
viewpropertyanimator.x(f);
}
return this;
}