com.dmcc.bid.util.AnimatorUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.dmcc.bid.util.AnimatorUtil.java

Source

/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dmcc.bid.util;

import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created on 2016/7/14.
 *
 * @author Yan Zhenjie.
 */
public class AnimatorUtil {

    public static final LinearOutSlowInInterpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new LinearOutSlowInInterpolator();

    // view
    public static void scaleShow(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
        view.setVisibility(View.VISIBLE);
        ViewCompat.animate(view).scaleX(1.0f).scaleY(1.0f).alpha(1.0f).setDuration(800)
                .setListener(viewPropertyAnimatorListener).setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR).start();
    }

    // ??view
    public static void scaleHide(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
        ViewCompat.animate(view).scaleX(0.0f).scaleY(0.0f).alpha(0.0f).setDuration(800)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR).setListener(viewPropertyAnimatorListener).start();
    }

    /**
     * ??
     * <p/>
     * ?view?
     * ?
     *
     * @param root
     */
    public static void addGlobaLayoutListener(final View root, final View childView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect rect = new Rect();
                // ?root?
                root.getWindowVisibleDisplayFrame(rect);
                // ?root??(View?)
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                // ??100
                if (rootInvisibleHeight > 100) {
                    //                            Logger.e("?");
                    int[] location = new int[2];
                    AnimatorUtil.scaleHide(childView, new ViewPropertyAnimatorListener() {
                        @Override
                        public void onAnimationStart(View view) {
                            childView.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationEnd(View view) {

                        }

                        @Override
                        public void onAnimationCancel(View view) {

                        }
                    });
                } else {
                    // ??
                    //                            Logger.e("");
                    AnimatorUtil.scaleShow(childView, new ViewPropertyAnimatorListener() {
                        @Override
                        public void onAnimationStart(View view) {

                        }

                        @Override
                        public void onAnimationEnd(View view) {

                            childView.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationCancel(View view) {

                        }
                    });
                }
            }
        });
    }

}