下面列出了android.view.View#animate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Animates a tab to be swiped horizontally.
*
* @param tabItem
* The tab item, which corresponds to the tab, which should be swiped, as an instance of
* the class {@link TabItem}. The tab item may not be null
* @param targetPosition
* The position on the x-axis, the tab should be moved to, in pixels as a {@link Float}
* value
* @param selected
* True, if the tab should become the selected one, false otherwise
* @param animationDuration
* The duration of the animation in milliseconds as a {@link Long} value
* @param velocity
* The velocity of the drag gesture, which caused the tab to be swiped, in pixels per
* second as a {@link Float} value
*/
private void animateSwipe(@NonNull final TabItem tabItem, final float targetPosition,
final boolean selected, final long animationDuration,
final float velocity) {
View view = contentViewRecycler.getView(tabItem.getTab());
if (view != null) {
float currentPosition = view.getX();
float distance = Math.abs(targetPosition - currentPosition);
float maxDistance = getTabSwitcher().getWidth() + swipedTabDistance;
long duration = velocity > 0 ? Math.round((distance / velocity) * 1000) :
Math.round(animationDuration * (distance / maxDistance));
ViewPropertyAnimator animation = view.animate();
animation.setListener(new AnimationListenerWrapper(
selected ? createSwipeSelectedTabAnimationListener(tabItem) :
createSwipeNeighborAnimationListener(tabItem)));
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.setDuration(duration);
animation.setStartDelay(0);
animation.x(targetPosition);
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 void bindView(View view){
if (view == null)
throw new NullPointerException("The view is cannot null");
this.view = view;
if (viewPropertyAnimator == null){
viewPropertyAnimator = view.animate();
viewPropertyAnimator.setDuration(300);
viewPropertyAnimator.setInterpolator(new LinearOutSlowInInterpolator());
}
}
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();
}
/**
* 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 the position, size and alpha of a specific tab in order to swipe it orthogonally.
*
* @param item
* The item, corresponds to the tab, which should be animated, as an instance of the
* class {@link AbstractItem}. The item may not be null
* @param remove
* True, if the tab should be removed after the animation has finished, false otherwise
* @param delayMultiplier
* The multiplied, which should be used to calculate the delay after which the animation
* should be started, by being multiplied with the default delay, as an {@link Integer}
* value
* @param swipeAnimation
* The animation, which should be used, as an instance of the class {@link
* SwipeAnimation}. The animation may not be null
* @param listener
* The listener, which should be notified about the progress of the animation, as an
* instance of the type {@link AnimatorListener} or null, if no listener should be
* notified
*/
private void animateSwipe(@NonNull final AbstractItem item, final boolean remove,
final int delayMultiplier,
@NonNull final SwipeAnimation swipeAnimation,
@Nullable final AnimatorListener listener) {
View view = item.getView();
float currentScale = getArithmetics().getScale(item, true);
float swipePosition = calculateSwipePosition();
float targetPosition = remove ?
(swipeAnimation.getDirection() == SwipeDirection.LEFT_OR_TOP ? -1 * swipePosition :
swipePosition) : 0;
float currentPosition = getArithmetics().getPosition(Axis.ORTHOGONAL_AXIS, item);
float distance = Math.abs(targetPosition - currentPosition);
long animationDuration = swipeAnimation.getDuration() != -1 ? swipeAnimation.getDuration() :
Math.round(swipeAnimationDuration * (distance / swipePosition));
ViewPropertyAnimator animation = view.animate();
animation.setInterpolator(
swipeAnimation.getInterpolator() != null ? swipeAnimation.getInterpolator() :
new AccelerateDecelerateInterpolator());
animation.setListener(new AnimationListenerWrapper(listener));
animation.setDuration(animationDuration);
getArithmetics()
.animatePosition(Axis.ORTHOGONAL_AXIS, animation, item, targetPosition, true);
getArithmetics().animateScale(Axis.ORTHOGONAL_AXIS, animation,
remove ? swipedTabScale * currentScale : currentScale);
getArithmetics().animateScale(Axis.DRAGGING_AXIS, animation,
remove ? swipedTabScale * currentScale : currentScale);
animation.alpha(remove ? swipedTabAlpha : 1);
animation.setStartDelay(delayMultiplier * calculateAnimationDelay(animationDuration));
animation.start();
}
private static void showView(View view) {
if (view != null) {
ViewPropertyAnimator animator = view.animate();
animator.cancel();
animator.alpha(1);
}
}
/**
* 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;
}
/**
* Creates and returns an animation listener, which allows to hide a tab, which has been added
* by using a peek animation, when the animation has been ended.
*
* @param item
* The item, which corresponds to the tab, which has been added by using the peek
* animation, as an instance of the class {@link AbstractItem}. The item may not be
* null
* @param peekAnimation
* The peek animation as an instance of the class {@link PeekAnimation}. The peek
* animation may not be null
* @return The listener, which has been created, as an instance of the type {@link
* AnimatorListener}. The listener may not be null
*/
@NonNull
private AnimatorListener createPeekAnimationListener(@NonNull final AbstractItem item,
@NonNull final PeekAnimation peekAnimation) {
return new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(final Animator animation) {
super.onAnimationEnd(animation);
long totalDuration =
peekAnimation.getDuration() != -1 ? peekAnimation.getDuration() :
peekAnimationDuration;
long duration = totalDuration / 3;
Interpolator interpolator =
peekAnimation.getInterpolator() != null ? peekAnimation.getInterpolator() :
new AccelerateDecelerateInterpolator();
View view = item.getView();
getArithmetics().setPivot(Axis.DRAGGING_AXIS, item, tabTitleContainerHeight);
getArithmetics().setPivot(Axis.ORTHOGONAL_AXIS, item,
getArithmetics().getSize(Axis.ORTHOGONAL_AXIS, item) / 2f);
ViewPropertyAnimator animator = view.animate();
animator.setDuration(duration);
animator.setStartDelay(duration);
animator.setInterpolator(interpolator);
animator.setListener(
new AnimationListenerWrapper(createRevertPeekAnimationListener(item)));
animator.alpha(0);
getArithmetics().animatePosition(Axis.DRAGGING_AXIS, animator, item,
getArithmetics().getPosition(Axis.DRAGGING_AXIS, item) * 1.5f);
getArithmetics().animateScale(Axis.DRAGGING_AXIS, animator, 0);
getArithmetics().animateScale(Axis.ORTHOGONAL_AXIS, animator, 0);
animator.start();
}
};
}
public static ViewPropertyAnimator fadeIn(View to, int duration) {
if (to.getVisibility() != View.VISIBLE) {
to.setAlpha(0f);
to.setVisibility(View.VISIBLE);
}
to.clearAnimation();
final ViewPropertyAnimator animator = to.animate();
animator.alpha(1f)
.setDuration(duration)
.setListener(null);
return animator;
}
private void startExitAnimation(final Runnable onAnimationNearlyEnded) {
Interpolator interpolator = new AccelerateInterpolator();
final View viewForAnimationNearlyEnded = mExitAnimationViews.get(5);
for (int i = 0; i < mExitAnimationViews.size(); ++i) {
final View v = mExitAnimationViews.get(i);
v.setAlpha(1f);
v.setTranslationY(0);
ViewPropertyAnimator animator = v.animate();
if (v == viewForAnimationNearlyEnded) {
animator.setListener(new AnimatorEndWithoutCancelListener() {
@Override
public void onAnimationEndWithoutCancel() {
onAnimationNearlyEnded.run();
}
});
}
animator
.withLayer()
.alpha(0.0f)
.translationY(-75)
.setInterpolator(interpolator)
.setStartDelay(mAnimStaggerDelay * i)
.setDuration(mAnimShortDuration)
.start();
}
mFavoriteBtn.animate()
.withLayer()
.scaleX(0f)
.scaleY(0f)
.setInterpolator(interpolator)
.setDuration(mAnimShortDuration)
.start();
}
public static void startAnimators(final View view, int startOffsetX, int startOffsetY, long delay) {
if (view.getVisibility() == View.VISIBLE && view.getAlpha() != 0f) {
view.clearAnimation();
view.animate().cancel();
final Resources res = view.getResources();
final float endAlpha = view.getAlpha();
final float endTranslateX = view.getTranslationX();
final float endTranslateY = view.getTranslationY();
view.setAlpha(0);
final Animator fade = ObjectAnimator.ofFloat(view, View.ALPHA, endAlpha);
fade.setDuration(res.getInteger(R.integer.material_in_fade_anim_duration));
fade.setInterpolator(new AccelerateInterpolator());
fade.setStartDelay(delay);
fade.start();
ViewPropertyAnimator slide = view.animate();
if (startOffsetY != 0) {
view.setTranslationY(startOffsetY);
slide.translationY(endTranslateY);
} else {
view.setTranslationX(startOffsetX);
slide.translationX(endTranslateX);
}
slide.setInterpolator(new DecelerateInterpolator(2));
slide.setDuration(res.getInteger(R.integer.material_in_slide_anim_duration));
slide.setStartDelay(delay);
slide.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
if (fade.isStarted()) {
fade.cancel();
}
view.setAlpha(endAlpha);
view.setTranslationX(endTranslateX);
view.setTranslationY(endTranslateY);
}
});
slide.start();
}
}
public static void hideView(View view) {
if (view != null) {
ViewPropertyAnimator animator = view.animate();
animator.cancel();
animator.alpha(0);
}
}
/**
* 增加我的频道
*
* @param v
*/
private void addMyChannel(final View v) {
//让点击的view置于最前方,避免遮挡
v.bringToFront();
ChannelAttr tag = getChannelAttr(v);
ArrayList<View> channels = channelGroups.get(tag.groupIndex);
ArrayList<View> myChannels = channelGroups.get(0);
View insertPositionChannel;
if (myChannels.size() == 0) {
insertPositionChannel = channelTitleGroups.get(0);
} else {
insertPositionChannel = myChannels.get(myChannels.size() - 1);
}
ChannelAttr insertPositionChannelTag = getChannelAttr(insertPositionChannel);
myChannels.add(myChannels.size(), v);
channels.remove(v);
v.setOnLongClickListener(this);
v.setOnTouchListener(this);
animateChangeGridLayoutHeight();
ViewPropertyAnimator animate = v.animate();
if (myChannels.size() % channelColumn == 1 || channelColumn == 1) {
if (myChannels.size() == 1) {
tag.coordinate = new PointF(insertPositionChannelTag.coordinate.x, insertPositionChannelTag.coordinate.y + insertPositionChannel.getMeasuredHeight());
//我的频道多一行,下面的view往下移
viewMove(1, channelHeight);
} else {
ChannelAttr firstMyChannelTag = getChannelAttr(myChannels.get(0));
tag.coordinate = new PointF(firstMyChannelTag.coordinate.x, insertPositionChannelTag.coordinate.y + channelHeight + channelHorizontalSpacing);
//我的频道多一行,下面的view往下移
viewMove(1, channelHeight + channelHorizontalSpacing);
}
} else {
tag.coordinate = new PointF(insertPositionChannelTag.coordinate.x + channelWidth + channelVerticalSpacing, insertPositionChannelTag.coordinate.y);
}
//可自定义插入位置,暂时在尾部插入
int insertPosition = getInsertPosition();
if (insertPosition != myChannels.size() - 1) {
backOrForward(v, insertPosition, myChannels.size() - 1, myChannels, tag, getChannelAttr(myChannels.get(insertPosition)));
}
animate.x(tag.coordinate.x).y(tag.coordinate.y).setDuration(DURATION_TIME);
if (channelClickType == DELETE) {
styleAdapter.setEditStyle(getViewHolder(v));
}
//该频道少一行,下面的view往上移
if (channels.size() % channelColumn == 0) {
if (channels.size() == 0) {
viewMove(tag.groupIndex + 1, -channelHeight);
} else {
viewMove(tag.groupIndex + 1, -channelHeight - channelHorizontalSpacing);
}
}
tag.groupIndex = 0;
}
/**
* Animates the position and size of a specific tab in order to show 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 type
* {@link Interpolator}. The interpolator may not be null
* @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 animateShowSwitcher(@NonNull final AbstractItem item, final long duration,
@NonNull final Interpolator interpolator,
@Nullable final AnimatorListener listener) {
View view = item.getView();
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
view.setX(layoutParams.leftMargin);
view.setY(layoutParams.topMargin);
getArithmetics().setScale(Axis.DRAGGING_AXIS, item, 1);
getArithmetics().setScale(Axis.ORTHOGONAL_AXIS, item, 1);
getArithmetics().setPivot(Axis.DRAGGING_AXIS, item,
getArithmetics().getPivot(Axis.DRAGGING_AXIS, item, DragState.NONE));
getArithmetics().setPivot(Axis.ORTHOGONAL_AXIS, item,
getArithmetics().getPivot(Axis.ORTHOGONAL_AXIS, item, DragState.NONE));
float scale = getArithmetics().getScale(item, true);
int selectedTabIndex = getModel().getSelectedTabIndex();
if (item.getIndex() < selectedTabIndex) {
getArithmetics().setPosition(Axis.DRAGGING_AXIS, item,
getArithmetics().getTabContainerSize(Axis.DRAGGING_AXIS));
} else if (item.getIndex() > selectedTabIndex) {
getArithmetics().setPosition(Axis.DRAGGING_AXIS, item,
getTabSwitcher().getLayout() == Layout.PHONE_LANDSCAPE ? 0 :
layoutParams.topMargin);
}
if (tabViewBottomMargin == -1) {
tabViewBottomMargin = calculateBottomMargin(item);
}
animateBottomMargin(view, tabViewBottomMargin, duration, 0);
ViewPropertyAnimator animation = view.animate();
animation.setDuration(duration);
animation.setInterpolator(interpolator);
animation.setListener(new AnimationListenerWrapper(listener));
getArithmetics().animateScale(Axis.DRAGGING_AXIS, animation, scale);
getArithmetics().animateScale(Axis.ORTHOGONAL_AXIS, animation, scale);
getArithmetics()
.animatePosition(Axis.DRAGGING_AXIS, animation, item, item.getTag().getPosition(),
true);
getArithmetics().animatePosition(Axis.ORTHOGONAL_AXIS, animation, item, 0, true);
animation.setStartDelay(0);
animation.start();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layoutId, null);
}
Friend friend = friends.get(position);
if (friend != null) {
// TODO Implement View holder pattern
TextView name = (TextView) v.findViewById(R.id.fragment_start_friendslist_item_name);
TextView amount = (TextView) v.findViewById(R.id.fragment_start_friendslist_item_amount);
final RoundedImageView pic = (RoundedImageView) v.findViewById(R.id.fragment_start_friendslist_item_pic);
v.setTag(friend); // Stored as a tag to be retrieved later for OnItemClickListener
Log.i(TAG, "Now retrieving contact image");
ImageLoader.getInstance().displayImage(friend.getLookupURI(), pic, Application.getImageOptions());
name.setText(friend.getName());
if (friend.getDebt().compareTo(BigDecimal.ZERO) < 0) {
pic.setOuterColor(mIOweThemColour);
amount.setText(SettingsFragment.getCurrencySymbol(mContext) +
SettingsFragment.getFormattedAmount(friend.getDebt().negate()));
} else {
pic.setOuterColor(mTheyOweMeColour);
amount.setText(SettingsFragment.getCurrencySymbol(mContext) +
SettingsFragment.getFormattedAmount(friend.getDebt()));
}
}
v.setScaleX(0f);
v.setScaleY(0f);
v.setAlpha(0f);
ViewPropertyAnimator animate = v.animate();
animate.scaleX(1f);
animate.scaleY(1f);
animate.alpha(1f);
animate.setDuration(500);
animate.start();
return v;
}
ViewCompatImpl(View view) {
super(view);
mViewPropertyAnimator = view.animate();
}
ViewPropertyAnimatorICS(View view) {
mNative = new WeakReference<android.view.ViewPropertyAnimator>(view.animate());
}
public PAnimation(View view) {
mView = view;
mAnim = view.animate();
}
ViewPropertyAnimatorICS(View view) {
mNative = new WeakReference<android.view.ViewPropertyAnimator>(view.animate());
}