AnimationUtil.java 6.52 KB
package com.sw.laryngoscope.utils;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

import com.sw.laryngoscope.R;

public class AnimationUtil {
 
    private final static String TAG = "AnimationUtils";

    private Context mContext;

    private View tmpSet;
    private TranslateAnimation mShowSetAction;
    private TranslateAnimation mCloseSetAction;

    private View tmpPreview;
    private TranslateAnimation mShowPreviewAction;
    private TranslateAnimation mClosePreviewAction;

    private Animation incAnimation;

    public AnimationUtil(Context context ) {
        mContext = context;
    }

    public void setTmpSet(View tmpSet) {
        this.tmpSet = tmpSet;
    }

    public void setTmpPreview(View tmpPreview) {
        this.tmpPreview = tmpPreview;
    }

    public void setAnimation() {
        //设置显示时的动画
        mShowSetAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        mShowSetAction.setDuration(300);
        //设置隐藏时的动画,监听动画结束后隐藏选择框
        mCloseSetAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        mCloseSetAction.setDuration(300);
        mCloseSetAction.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //
                tmpSet.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


        //设置显示时的动画
        mShowPreviewAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowPreviewAction.setDuration(300);
        //设置隐藏时的动画,监听动画结束后隐藏选择框
        mClosePreviewAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mClosePreviewAction.setDuration(300);
        mClosePreviewAction.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //
                tmpPreview.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        incAnimation = AnimationUtils.loadAnimation(mContext, R.anim.small_animation);
    }

    //显示
    public void showSetView() {
        if ( View.GONE == tmpSet.getVisibility() ) {
            tmpSet.setVisibility(View.VISIBLE);//tmp背景色是暗的,所以看起来像是背景变暗
            tmpSet.startAnimation(mShowSetAction);//为选择框设置动画
        }
    }

    //关闭
    public void closeSetView() {
        if ( View.VISIBLE == tmpSet.getVisibility() ) {
            tmpSet.startAnimation(mCloseSetAction);
        }
    }

    //显示
    public void showPreview() {
        if ( View.GONE == tmpPreview.getVisibility() ) {
            tmpPreview.setVisibility(View.VISIBLE);//tmp背景色是暗的,所以看起来像是背景变暗
            tmpPreview.startAnimation(mShowPreviewAction);//为选择框设置动画
        }
    }

    //关闭
    public void closePreview() {
        if ( View.VISIBLE == tmpPreview.getVisibility() ) {
            tmpPreview.startAnimation(mClosePreviewAction);
        }
    }

    public void incFileAnim(View view) {
        view.startAnimation(incAnimation);//开始动画
        incAnimation.setAnimationListener(new Animation.AnimationListener(){
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                view.clearAnimation();
            }
        });
    }

    private void setFlickerAnimation(View iv_chat_head) {
        final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
        animation.setDuration(500); // duration - half a second
        animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
        animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
        animation.setRepeatMode(Animation.REVERSE); //
        iv_chat_head.setAnimation(animation);
    }

    private static final int MAX_LEVEL = 90;

    public static void animateArrow(View view, boolean shouldRotateUp) {
        int start = shouldRotateUp ? 0 : MAX_LEVEL;
        int end = shouldRotateUp ? MAX_LEVEL : 0;
        ObjectAnimator visibleToInVisable = ObjectAnimator.ofFloat(view, "rotation",
                start, end);
//        visibleToInVisable.setInterpolator(new LinearOutSlowInInterpolator());
        visibleToInVisable.setInterpolator(new AccelerateInterpolator());
        visibleToInVisable.setDuration(50);
        visibleToInVisable.start();
    }

    /*private Drawable arrowDrawable;
    private ObjectAnimator arrowAnimator = null;
    private RotateAnimation rotate;*/
    //@SuppressLint("ResourceType")
        /*Animation animation = AnimationUtils.loadAnimation(getContext(),R.drawable.arrow);
        img_lan_next.startAnimation(animation);*/

        /*arrowAnimator = ObjectAnimator.ofInt(
                getContext().getDrawable(R.drawable.arrow), "level", start, end);*/

        /*rotate = new RotateAnimation(0, 90,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(1000);
        img_lan_next.startAnimation(rotate);*/

 }