Example usage for android.view ViewPropertyAnimator setListener

List of usage examples for android.view ViewPropertyAnimator setListener

Introduction

In this page you can find the example usage for android.view ViewPropertyAnimator setListener.

Prototype

public ViewPropertyAnimator setListener(Animator.AnimatorListener listener) 

Source Link

Document

Sets a listener for events in the underlying Animators that run the property animations.

Usage

From source file:Main.java

public static void scale(final View view, float fromScale, float toScale, long duration,
        final Runnable whenDone) {
    if (Build.VERSION.SDK_INT >= 12) {
        if (duration == 0) {
            view.setScaleX(toScale);//  w w w. j a va 2  s. c  o  m
            view.setScaleY(toScale);
            if (whenDone != null)
                whenDone.run();
        } else {
            ViewPropertyAnimator animation = view.animate().scaleX(toScale).scaleY(toScale)
                    .setDuration(duration);
            if (whenDone != null) {
                animation.setListener(new AnimatorListener() {
                    @Override
                    public void onAnimationCancel(Animator animation) {
                        whenDone.run();
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        whenDone.run();
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                });
            }
            animation.start();
        }
    } else {
        ScaleAnimation scale = new ScaleAnimation(fromScale, toScale, fromScale, toScale,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(duration);
        scale.setFillEnabled(true);
        scale.setFillBefore(true);
        scale.setFillAfter(true);

        if (whenDone != null) {
            scale.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    whenDone.run();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationStart(Animation animation) {
                }

            });
        }
        addAnimation(view, scale);
    }
}

From source file:com.grarak.kerneladiutor.views.recyclerview.overallstatistics.FrequencyButtonView.java

private void rotate(final View v, boolean reverse) {
    ViewPropertyAnimator animator = v.animate().rotation(reverse ? -360 : 360);
    animator.setListener(new AnimatorListenerAdapter() {
        @Override/* www .j a  va  2s  .  c  o m*/
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            v.setRotation(0);
        }
    });
    animator.start();
}

From source file:com.guodong.sun.guodong.behavior.MyFabBehavior.java

private void show(final View view) {
    ViewPropertyAnimator animator = view.animate().translationY(0).setInterpolator(INTERPOLATOR)
            .setDuration(200);//from  w  ww  . j  a  va  2s  . co m
    animator.setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {
            view.setVisibility(View.VISIBLE);
            isAnimate = true;
        }

        @Override
        public void onAnimationEnd(Animator animator) {
            isAnimate = false;
        }

        @Override
        public void onAnimationCancel(Animator animator) {
            hide(view);
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    animator.start();
}

From source file:com.guodong.sun.guodong.behavior.MyFabBehavior.java

private void hide(final View view) {
    ViewPropertyAnimator animator = view.animate().translationY(viewY).setInterpolator(INTERPOLATOR)
            .setDuration(200);/*from   w  w w  . j  a  v  a  2  s .  c o m*/

    animator.setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {
            isAnimate = true;
        }

        @Override
        public void onAnimationEnd(Animator animator) {
            view.setVisibility(View.GONE);
            isAnimate = false;
        }

        @Override
        public void onAnimationCancel(Animator animator) {
            show(view);
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    animator.start();
}

From source file:com.hybris.mobile.lib.ui.view.Alert.java

/**
 * Show the alert/*from  w  ww .  j  a v a  2  s  . c  om*/
 *
 * @param context                 application-specific resources
 * @param configuration           describes all device configuration information
 * @param text                    message to be displayed
 * @param forceClearPreviousAlert true will clear previous alert else keep it
 */
@SuppressLint("NewApi")
private static void showAlertOnScreen(final Activity context, final Configuration configuration,
        final String text, boolean forceClearPreviousAlert) {

    final ViewGroup mainView = ((ViewGroup) context.findViewById(android.R.id.content));
    boolean currentlyDisplayed = false;
    int viewId = R.id.alert_view_top;
    final TextView textView;
    boolean alertAlreadyExists = false;

    if (configuration.getOrientation().equals(Configuration.Orientation.BOTTOM)) {
        viewId = R.id.alert_view_bottom;
    }

    // Retrieving the view
    RelativeLayout relativeLayout = (RelativeLayout) mainView.findViewById(viewId);

    if (forceClearPreviousAlert) {
        mainView.removeView(relativeLayout);
        relativeLayout = null;
    }

    // Creating the view
    if (relativeLayout == null) {

        // Main layout
        relativeLayout = new RelativeLayout(context);
        relativeLayout.setId(viewId);
        relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, configuration.getHeight()));
        relativeLayout.setGravity(Gravity.CENTER);

        // Textview
        textView = new TextView(context);
        textView.setId(R.id.alert_view_text);
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(
                new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        relativeLayout.addView(textView);

        setIcon(context, configuration, relativeLayout, textView);

        if (configuration.getOrientation().equals(Configuration.Orientation.TOP)) {
            relativeLayout.setY(-configuration.getHeight());
        } else {
            relativeLayout.setY(mainView.getHeight());
        }

        // Adding the view to the global layout
        mainView.addView(relativeLayout, 0);
        relativeLayout.bringToFront();
        relativeLayout.requestLayout();
        relativeLayout.invalidate();
    }
    // View already exists
    else {
        alertAlreadyExists = true;
        textView = (TextView) relativeLayout.findViewById(R.id.alert_view_text);

        if (configuration.getOrientation().equals(Configuration.Orientation.TOP)) {
            if (relativeLayout.getY() == 0) {
                currentlyDisplayed = true;
            }
        } else {
            if (relativeLayout.getY() < mainView.getHeight()) {
                currentlyDisplayed = true;
            }
        }

        // The view is currently shown to the user
        if (currentlyDisplayed) {

            // If the message is not the same, we hide the current message and display the new one
            if (!StringUtils.equals(text, textView.getText())) {
                // Anim out the current message
                ViewPropertyAnimator viewPropertyAnimator = animOut(configuration, mainView, relativeLayout);

                final RelativeLayout relativeLayoutFinal = relativeLayout;

                if (viewPropertyAnimator != null) {
                    // Anim in the new message after the animation out has finished
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        viewPropertyAnimator.setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                setIcon(context, configuration, relativeLayoutFinal, textView);
                                animIn(configuration, relativeLayoutFinal, textView, mainView, text);
                            }
                        });
                    } else {
                        viewPropertyAnimator.withEndAction(new Runnable() {
                            @Override
                            public void run() {
                                setIcon(context, configuration, relativeLayoutFinal, textView);
                                animIn(configuration, relativeLayoutFinal, textView, mainView, text);
                            }
                        });
                    }
                } else {
                    setIcon(context, configuration, relativeLayoutFinal, textView);
                    animIn(configuration, relativeLayoutFinal, textView, mainView, text);
                }
            }
        }
    }

    final RelativeLayout relativeLayoutFinal = relativeLayout;

    // Close the alert by clicking the layout
    if (configuration.isCloseable()) {
        relativeLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                animOut(configuration, mainView, relativeLayoutFinal);
                v.performClick();
                return true;
            }
        });
    }

    if (!currentlyDisplayed) {
        // Set the icon in case the alert already exists but it's not currently displayed
        if (alertAlreadyExists) {
            setIcon(context, configuration, relativeLayoutFinal, textView);
        }

        // We anim in the alert
        animIn(configuration, relativeLayoutFinal, textView, mainView, text);
    }
}

From source file:com.tapcentive.sdk.actions.TapMessageFragment.java

/**
 * Animate the game.//from   w ww . j  a va  2 s .c om
 * 
 * @param view
 *            the view
 * @param gameLayout
 *            the game layout to animate
 * @param duration
 *            the duration of the animation in ms
 */
@SuppressLint("NewApi")
private void animateGame(final View view) {
    // stop screen from sleeping
    getActivity().getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.winning_layout_portrait);
    ViewPropertyAnimator animator = layout.animate();
    animator.translationYBy(-(1 + usableHeight)).setDuration(200);

    animator.setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            TapcentiveLibrary.getInstance().setAnimatedWhenStopped(false);
            TapcentiveLibrary.getInstance().setAnimationInProcess(false);
        }
    });
}

From source file:org.sufficientlysecure.keychain.ui.ViewKeyAdvActivity.java

private void animateMenuItem(final MenuItem vEditSubkeys, final boolean animateShow) {

    View actionView = LayoutInflater.from(this).inflate(R.layout.edit_icon, null);
    vEditSubkeys.setActionView(actionView);
    actionView.setTranslationX(animateShow ? 150 : 0);

    ViewPropertyAnimator animator = actionView.animate();
    animator.translationX(animateShow ? 0 : 150);
    animator.setDuration(300);/* www  .ja v a  2s.  com*/
    animator.setInterpolator(new OvershootInterpolator(1.5f));
    animator.setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            if (!animateShow) {
                vEditSubkeys.setVisible(false);
            }
            vEditSubkeys.setActionView(null);
        }
    });
    animator.start();

}

From source file:br.marcha.jesus.youtube.VideoListActivity.java

@TargetApi(16)
private void runOnAnimationEnd(ViewPropertyAnimator animator, final Runnable runnable) {
    if (Build.VERSION.SDK_INT >= 16) {
        animator.withEndAction(runnable);
    } else {/*from w  ww. j a va2s.com*/
        animator.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                runnable.run();
            }
        });
    }
}

From source file:co.com.parsoniisolutions.custombottomsheetbehavior.lib.MergedAppBarLayoutBehavior.java

private boolean setToolbarVisible(boolean visible, final View child) {
    ViewPropertyAnimator mAppBarLayoutAnimation;
    boolean childMoved = false;
    if (visible && !mVisible) {
        childMoved = true;//from ww w .  j  av a2 s.c  o  m
        child.setY(-child.getHeight() / 3);
        mAppBarLayoutAnimation = child.animate()
                .setDuration(mContext.getResources().getInteger(android.R.integer.config_shortAnimTime));
        mAppBarLayoutAnimation.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                child.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                ((AppCompatActivity) mContext).setSupportActionBar(mToolbar);
                mToolbar.setNavigationOnClickListener(mOnNavigationClickListener);
                ActionBar actionBar = ((AppCompatActivity) mContext).getSupportActionBar();
                if (actionBar != null) {
                    actionBar.setDisplayHomeAsUpEnabled(true);
                }
                mVisible = true;
            }
        });
        mAppBarLayoutAnimation.alpha(1).y(mInitialY).start();
    } else if (!visible && mVisible) {
        mAppBarLayoutAnimation = child.animate()
                .setDuration(mContext.getResources().getInteger(android.R.integer.config_shortAnimTime));
        mAppBarLayoutAnimation.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                child.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                ((AppCompatActivity) mContext).setSupportActionBar(null);
                mVisible = false;
            }
        });
        mAppBarLayoutAnimation.alpha(0).start();
    }

    return childMoved;
}

From source file:com.bottomsheetbehavior.MergedAppBarLayoutBehavior.java

private boolean setToolbarVisible(boolean visible, final View child) {
    ViewPropertyAnimator mAppBarLayoutAnimation;
    boolean childMoved = false;
    if (visible && !mVisible) {
        childMoved = true;//w  w  w.  j  ava2 s. c o m
        child.setY(-child.getHeight() / 3);
        mAppBarLayoutAnimation = child.animate()
                .setDuration(mContext.getResources().getInteger(android.R.integer.config_shortAnimTime));
        mAppBarLayoutAnimation.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                child.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mVisible = true;
            }
        });
        mAppBarLayoutAnimation.alpha(1).y(mInitialY).start();
    } else if (!visible && mVisible) {
        mAppBarLayoutAnimation = child.animate()
                .setDuration(mContext.getResources().getInteger(android.R.integer.config_shortAnimTime));
        mAppBarLayoutAnimation.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                child.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mVisible = false;
            }
        });
        mAppBarLayoutAnimation.alpha(0).start();
    }

    return childMoved;
}