下面列出了android.animation.AnimatorSet#Builder ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Constructs an animator for the specified property on a group of views.
* See {@link ObjectAnimator#ofFloat(Object, String, float...)} for
* implementation details.
*
* @param property The property being animated.
* @param value The value to which that property should animate.
* @param views The target views to animate.
* @return An animator for all the specified views.
*/
private static Animator groupAnimatorOfFloat(
Property<View, Float> property, float value, View... views) {
AnimatorSet animSet = new AnimatorSet();
AnimatorSet.Builder builder = null;
for (int i = views.length - 1; i >= 0; i--) {
final Animator anim = ObjectAnimator.ofFloat(views[i], property, value);
if (builder == null) {
builder = animSet.play(anim);
} else {
builder.with(anim);
}
}
return animSet;
}
private AnimatorSet buildSetForBodies(List<CustomAnimationBody> animations, View view) {
final AnimatorSet animatorSet = new AnimatorSet();
if (animations.size() > 0) {
final Iterator<CustomAnimationBody> iterator = animations.iterator();
final AnimatorSet.Builder builder =
animatorSet.play(iterator.next().createFor(view));
while (iterator.hasNext()) {
builder.with(iterator.next().createFor(view));
}
}
return animatorSet;
}
private void runMenuItemEnterAnimations() {
mMenuItemEnterAnimator = new AnimatorSet();
AnimatorSet.Builder builder = null;
ViewGroup list = mPopup.getListView();
for (int i = 0; i < list.getChildCount(); i++) {
View view = list.getChildAt(i);
Object animatorObject = view.getTag(R.id.menu_item_enter_anim_id);
if (animatorObject != null) {
if (builder == null) {
builder = mMenuItemEnterAnimator.play((Animator) animatorObject);
} else {
builder.with((Animator) animatorObject);
}
}
}
mMenuItemEnterAnimator.addListener(mAnimationHistogramRecorder);
mMenuItemEnterAnimator.start();
}
private void runMenuItemEnterAnimations() {
mMenuItemEnterAnimator = new AnimatorSet();
AnimatorSet.Builder builder = null;
ViewGroup list = mPopup.getListView();
for (int i = 0; i < list.getChildCount(); i++) {
View view = list.getChildAt(i);
Object animatorObject = view.getTag(R.id.menu_item_enter_anim_id);
if (animatorObject != null) {
if (builder == null) {
builder = mMenuItemEnterAnimator.play((Animator) animatorObject);
} else {
builder.with((Animator) animatorObject);
}
}
}
mMenuItemEnterAnimator.addListener(mAnimationHistogramRecorder);
mMenuItemEnterAnimator.start();
}
private void runMenuItemEnterAnimations() {
mMenuItemEnterAnimator = new AnimatorSet();
AnimatorSet.Builder builder = null;
ViewGroup list = mPopup.getListView();
for (int i = 0; i < list.getChildCount(); i++) {
View view = list.getChildAt(i);
Object animatorObject = view.getTag(R.id.menu_item_enter_anim_id);
if (animatorObject != null) {
if (builder == null) {
builder = mMenuItemEnterAnimator.play((Animator) animatorObject);
} else {
builder.with((Animator) animatorObject);
}
}
}
mMenuItemEnterAnimator.addListener(mAnimationHistogramRecorder);
mMenuItemEnterAnimator.start();
}
public void startTogetherByLink(Animator.AnimatorListener listener, AnimatorValue...animatorValues){
if(animationSet==null){
Log.d(TAG, "δ�ܳ�ʼ��");
return;
}
animationSet.removeAllListeners();
if(listener!=null){
animationSet.addListener(listener);
}
if (animatorValues != null) {
AnimatorSet.Builder lastBuilder=null;
for (int i = 0; i < animatorValues.length; i++) {
AnimatorSet.Builder curBuilder = animationSet.play(animatorValues[i].getAnimator());
if(animatorValues[i].getBeforeAnimator()!=null){
curBuilder.after(animatorValues[i].getBeforeAnimator());
}else if(lastBuilder!=null){
lastBuilder.with(animatorValues[i].getAnimator());
}
lastBuilder=curBuilder;
}
}else{
Log.d(TAG, "���������ǿ�");
return;
}
//animationSet.setDuration(duration/animatorValues.length);
animationSet.setInterpolator(interpolator);
animationSet.start();
}
@Override
protected Animator createSoftwareExit() {
final AnimatorSet set = new AnimatorSet();
// Linear exit after enter is completed.
final ObjectAnimator exit = ObjectAnimator.ofFloat(this, RippleBackground.OPACITY, 0);
exit.setInterpolator(LINEAR_INTERPOLATOR);
exit.setDuration(OPACITY_EXIT_DURATION);
AnimatorsCompat.setAutoCancel(exit);
//exit.setAutoCancel(true);
final AnimatorSet.Builder builder = set.play(exit);
// Linear "fast" enter based on current opacity.
final int fastEnterDuration = (int) ((1 - mOpacity) * OPACITY_ENTER_DURATION_FAST);
if (fastEnterDuration > 0) {
final ObjectAnimator enter = ObjectAnimator.ofFloat(this, RippleBackground.OPACITY, 1);
enter.setInterpolator(LINEAR_INTERPOLATOR);
enter.setDuration(fastEnterDuration);
AnimatorsCompat.setAutoCancel(enter);
//enter.setAutoCancel(true);
builder.after(enter);
}
return set;
}
private void animate(Interpolator interpolator, long duration, ValueAnimator first, ValueAnimator... animators) {
animator = new AnimatorSet();
animator.setDuration(duration);
animator.setInterpolator(interpolator);
animator.addListener(animatorListener);
AnimatorSet.Builder builder = animator.play(first);
for(ValueAnimator valueAnimator : animators) {
builder.with(valueAnimator);
}
animator.start();
}
public void show() {
if (mButtonView == null) {
throw new RuntimeException("请在设置点击按钮");
}
final int size = getChildCount() - 1;
if (size == 0) {
throw new RuntimeException("请在设置子菜单");
}
isOpen = true;
ObjectAnimator rotation = ObjectAnimator.ofFloat(mButtonView, "rotation", 0, 45);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(ANIMATION_DURATION);
AnimatorSet.Builder builder = animatorSet.play(rotation);
for (int i = 0; i < size; i++) {
PointF point = new PointF();
int avgAngle = (90 / (size - 1));
int angle = avgAngle * i;
switch (DIRECTION) {
case RIGHT_BOTTOM:
point.x = (float) Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) Math.sin(angle * Math.PI / 180) * RADIUS;
break;
case LEFT_BOTTOM:
point.x = (float) -Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) Math.sin(angle * Math.PI / 180) * RADIUS;
break;
case RIGHT_TOP:
point.x = (float) Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) -Math.sin(angle * Math.PI / 180) * RADIUS;
break;
case LEFT_TOP:
point.x = (float) -Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) -Math.sin(angle * Math.PI / 180) * RADIUS;
break;
default:
point.x = (float) -Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) -Math.sin(angle * Math.PI / 180) * RADIUS;
break;
}
View child = getChildAt(i);
ObjectAnimator translationX = ObjectAnimator.ofFloat(child, "translationX", 0, point.x);
ObjectAnimator alpha = ObjectAnimator.ofFloat(child, "alpha", 0, 1.0f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(child, "scaleX", 0, 1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(child, "scaleY", 0, 1.0f);
ObjectAnimator translationY = ObjectAnimator.ofFloat(child, "translationY", 0, point.y);
builder.with(translationX).with(translationY).with(alpha).with(scaleX).with(scaleY);
}
animatorSet.start();
}
public void close() {
if (mButtonView == null) {
throw new RuntimeException("请在设置点击按钮");
}
final int size = getChildCount() - 1;
if (size == 0) {
throw new RuntimeException("请在设置子菜单");
}
isOpen = false;
ObjectAnimator rotation = ObjectAnimator.ofFloat(mButtonView, "rotation", 45, 0);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(ANIMATION_DURATION);
AnimatorSet.Builder builder = animatorSet.play(rotation);
for (int i = 0; i < size; i++) {
PointF point = new PointF();
int avgAngle = (90 / (size - 1));
int angle = avgAngle * i;
switch (DIRECTION) {
case RIGHT_BOTTOM:
point.x = (float) Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) Math.sin(angle * Math.PI / 180) * RADIUS;
break;
case LEFT_BOTTOM:
point.x = (float) -Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) Math.sin(angle * Math.PI / 180) * RADIUS;
break;
case RIGHT_TOP:
point.x = (float) Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) -Math.sin(angle * Math.PI / 180) * RADIUS;
break;
case LEFT_TOP:
point.x = (float) -Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) -Math.sin(angle * Math.PI / 180) * RADIUS;
break;
default:
point.x = (float) -Math.cos(angle * Math.PI / 180) * RADIUS;
point.y = (float) -Math.sin(angle * Math.PI / 180) * RADIUS;
break;
}
View child = getChildAt(i);
ObjectAnimator translationX = ObjectAnimator.ofFloat(child, "translationX", point.x, 0);
ObjectAnimator alpha = ObjectAnimator.ofFloat(child, "alpha", 1.0f, 0);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(child, "scaleX", 1.0f, 0);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(child, "scaleY", 1.0f, 0);
ObjectAnimator translationY = ObjectAnimator.ofFloat(child, "translationY", point.y, 0);
builder.with(translationX).with(translationY).with(alpha).with(scaleX).with(scaleY);
}
animatorSet.start();
}
private void buildAnimViewWithDrawableResAndStart() {
//构造出 动画的承载体View,并添加进本容器View
IBezierAnimControler animControler = this.animControler;
View theHoldAnimView = null;
if (animControler != null) {
theHoldAnimView = animControler.provideHoldAnimView(this);
// if (theHoldAnimView instanceof ImageView) {
// ((ImageView) theHoldAnimView).setImageResource(willAnimDrawableRes);
// }
}
if (theHoldAnimView == null) {
ImageView holdAnimView = new ImageView(getContext());
holdAnimView.setImageResource(willAnimDrawableRes);
theHoldAnimView = holdAnimView;
}
ViewGroup.LayoutParams vlp = theHoldAnimView.getLayoutParams();
addView(theHoldAnimView, vlp != null ? vlp : flp4ChildView);
//构造初始化 show出的动画,如果有的话
Animator animatorOfAnimViewBorn = null;
if (animControler != null) {
animatorOfAnimViewBorn = animControler.provideFirstShowAnimator(this, theHoldAnimView);
}
if (animatorOfAnimViewBorn == null && isWillUseDefFirstShowAnimator) {
animatorOfAnimViewBorn = buildDefBornViewAnimator(theHoldAnimView, durationOfAnimViewBorn);
}
//贝塞尔曲线动画
ValueAnimator bezierAnimator = buildBezierAnimator(theHoldAnimView);
//动画合集
AnimatorSet wholeAnimatorSet = new AnimatorSet();
AnimatorSet.Builder animsBuilder = wholeAnimatorSet.play(bezierAnimator);
//是否有提供和贝塞尔曲线动画一起执行的动画
Animator willPlayWithBezierAnim = null;
if (animControler != null) {
willPlayWithBezierAnim = animControler.provideAnimatorPlayWithBezierAnim(this, theHoldAnimView);
if (willPlayWithBezierAnim != null) {
//执行时间一定要和贝塞尔曲线动画 执行时间一致
willPlayWithBezierAnim.setDuration(this.beziaAnimatorDuration);
animsBuilder.with(willPlayWithBezierAnim);
}
}
//是否要在初始展示的动画之后再执行贝塞尔曲线动画
if (animatorOfAnimViewBorn != null) {
animsBuilder.after(animatorOfAnimViewBorn);
}
wholeAnimatorSet.setTarget(theHoldAnimView);
wholeAnimatorSet.start();
}