Back to project page android-utils.
The source code is released under:
Apache License
If you think the Android project android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2014 Zhenguo Jin (jinzhenguo1990@gmail.com) *//w ww .j ava 2 s . c o m * 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.worthed.util; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.CycleInterpolator; import android.view.animation.TranslateAnimation; /** * ????????????????????????????? * * @author zhenguo */ public class ViewAnimationUtils { // /** // * ????????? // */ // public static final long DEFAULT_ANIMATION_DURATION = 300; /* * ************************************************************* ????????????? * ******************************************************************** */ /** * ?????????????view.setVisibility(View.INVISIBLE)? * * @param view ???????? * @param isBanClick ?????????????????? * @param durationMillis ???????? * @param animationListener ??????? */ public static void invisibleViewByAlpha(final View view, long durationMillis, final boolean isBanClick, final AnimationListener animationListener) { if (view.getVisibility() != View.INVISIBLE) { view.setVisibility(View.INVISIBLE); AlphaAnimation hiddenAlphaAnimation = AnimationUtils .getHiddenAlphaAnimation(durationMillis); hiddenAlphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if (isBanClick) { view.setClickable(false); } if (animationListener != null) { animationListener.onAnimationStart(animation); } } @Override public void onAnimationRepeat(Animation animation) { if (animationListener != null) { animationListener.onAnimationRepeat(animation); } } @Override public void onAnimationEnd(Animation animation) { if (isBanClick) { view.setClickable(true); } if (animationListener != null) { animationListener.onAnimationEnd(animation); } } }); view.startAnimation(hiddenAlphaAnimation); } } /** * ?????????????view.setVisibility(View.INVISIBLE)? * * @param view ???????? * @param durationMillis ???????? * @param animationListener ??????? */ public static void invisibleViewByAlpha(final View view, long durationMillis, final AnimationListener animationListener) { invisibleViewByAlpha(view, durationMillis, false, animationListener); } /** * ?????????????view.setVisibility(View.INVISIBLE)? * * @param view ???????? * @param durationMillis ???????? * @param isBanClick ?????????????????? */ public static void invisibleViewByAlpha(final View view, long durationMillis, boolean isBanClick) { invisibleViewByAlpha(view, durationMillis, isBanClick, null); } /** * ?????????????view.setVisibility(View.INVISIBLE)? * * @param view ???????? * @param durationMillis ???????? */ public static void invisibleViewByAlpha(final View view, long durationMillis) { invisibleViewByAlpha(view, durationMillis, false, null); } /** * ?????????????view.setVisibility(View.INVISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param isBanClick ?????????????????? * @param animationListener ??????? */ public static void invisibleViewByAlpha(final View view, boolean isBanClick, final AnimationListener animationListener) { invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, isBanClick, animationListener); } /** * ?????????????view.setVisibility(View.INVISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param animationListener ??????? */ public static void invisibleViewByAlpha(final View view, final AnimationListener animationListener) { invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false, animationListener); } /** * ?????????????view.setVisibility(View.INVISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param isBanClick ?????????????????? */ public static void invisibleViewByAlpha(final View view, boolean isBanClick) { invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, isBanClick, null); } /** * ?????????????view.setVisibility(View.INVISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? */ public static void invisibleViewByAlpha(final View view) { invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false, null); } /** * ?????????????????????????view.setVisibility(View.GONE)? * * @param view ???????? * @param durationMillis ???????? * @param isBanClick ?????????????????? * @param animationListener ??????? */ public static void goneViewByAlpha(final View view, long durationMillis, final boolean isBanClick, final AnimationListener animationListener) { if (view.getVisibility() != View.GONE) { view.setVisibility(View.GONE); AlphaAnimation hiddenAlphaAnimation = AnimationUtils .getHiddenAlphaAnimation(durationMillis); hiddenAlphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if (isBanClick) { view.setClickable(false); } if (animationListener != null) { animationListener.onAnimationStart(animation); } } @Override public void onAnimationRepeat(Animation animation) { if (animationListener != null) { animationListener.onAnimationRepeat(animation); } } @Override public void onAnimationEnd(Animation animation) { if (isBanClick) { view.setClickable(true); } if (animationListener != null) { animationListener.onAnimationEnd(animation); } } }); view.startAnimation(hiddenAlphaAnimation); } } /** * ?????????????????????????view.setVisibility(View.GONE)? * * @param view ???????? * @param durationMillis ???????? * @param animationListener ??????? */ public static void goneViewByAlpha(final View view, long durationMillis, final AnimationListener animationListener) { goneViewByAlpha(view, durationMillis, false, animationListener); } /** * ?????????????????????????view.setVisibility(View.GONE)? * * @param view ???????? * @param durationMillis ???????? * @param isBanClick ?????????????????? */ public static void goneViewByAlpha(final View view, long durationMillis, final boolean isBanClick) { goneViewByAlpha(view, durationMillis, isBanClick, null); } /** * ?????????????????????????view.setVisibility(View.GONE)? * * @param view ???????? * @param durationMillis ???????? */ public static void goneViewByAlpha(final View view, long durationMillis) { goneViewByAlpha(view, durationMillis, false, null); } /** * ?????????????????????????view.setVisibility(View.GONE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param isBanClick ?????????????????? * @param animationListener ??????? */ public static void goneViewByAlpha(final View view, final boolean isBanClick, final AnimationListener animationListener) { goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, isBanClick, animationListener); } /** * ?????????????????????????view.setVisibility(View.GONE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param animationListener ??????? */ public static void goneViewByAlpha(final View view, final AnimationListener animationListener) { goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false, animationListener); } /** * ?????????????????????????view.setVisibility(View.GONE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param isBanClick ?????????????????? */ public static void goneViewByAlpha(final View view, final boolean isBanClick) { goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, isBanClick, null); } /** * ?????????????????????????view.setVisibility(View.GONE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? */ public static void goneViewByAlpha(final View view) { goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false, null); } /** * ????????????????view.setVisibility(View.VISIBLE)? * * @param view ???????? * @param durationMillis ???????? * @param isBanClick ?????????????????? * @param animationListener ??????? */ public static void visibleViewByAlpha(final View view, long durationMillis, final boolean isBanClick, final AnimationListener animationListener) { if (view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); AlphaAnimation showAlphaAnimation = AnimationUtils .getShowAlphaAnimation(durationMillis); showAlphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if (isBanClick) { view.setClickable(false); } if (animationListener != null) { animationListener.onAnimationStart(animation); } } @Override public void onAnimationRepeat(Animation animation) { if (animationListener != null) { animationListener.onAnimationRepeat(animation); } } @Override public void onAnimationEnd(Animation animation) { if (isBanClick) { view.setClickable(true); } if (animationListener != null) { animationListener.onAnimationEnd(animation); } } }); view.startAnimation(showAlphaAnimation); } } /** * ????????????????view.setVisibility(View.VISIBLE)? * * @param view ???????? * @param durationMillis ???????? * @param animationListener ??????? */ public static void visibleViewByAlpha(final View view, long durationMillis, final AnimationListener animationListener) { visibleViewByAlpha(view, durationMillis, false, animationListener); } /** * ????????????????view.setVisibility(View.VISIBLE)? * * @param view ???????? * @param durationMillis ???????? * @param isBanClick ?????????????????? */ public static void visibleViewByAlpha(final View view, long durationMillis, final boolean isBanClick) { visibleViewByAlpha(view, durationMillis, isBanClick, null); } /** * ????????????????view.setVisibility(View.VISIBLE)? * * @param view ???????? * @param durationMillis ???????? */ public static void visibleViewByAlpha(final View view, long durationMillis) { visibleViewByAlpha(view, durationMillis, false, null); } /** * ????????????????view.setVisibility(View.VISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param animationListener ??????? * @param isBanClick ?????????????????? */ public static void visibleViewByAlpha(final View view, final boolean isBanClick, final AnimationListener animationListener) { visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, isBanClick, animationListener); } /** * ????????????????view.setVisibility(View.VISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param animationListener ??????? */ public static void visibleViewByAlpha(final View view, final AnimationListener animationListener) { visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false, animationListener); } /** * ????????????????view.setVisibility(View.VISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? * @param isBanClick ?????????????????? */ public static void visibleViewByAlpha(final View view, final boolean isBanClick) { visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, isBanClick, null); } /** * ????????????????view.setVisibility(View.VISIBLE)?? * ?????????DEFAULT_ALPHA_ANIMATION_DURATION * * @param view ???????? */ public static void visibleViewByAlpha(final View view) { visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false, null); } /* * ************************************************************* ?????? * ******************************************************************** */ /** * ???? * * @param view ??????? * @param fromXDelta X??????? * @param toXDelta X????????? * @param fromYDelta Y??????? * @param toYDelta Y????????? * @param cycles ???? * @param durationMillis ????? * @param isBanClick ?????????????????? */ public static void translate(final View view, float fromXDelta, float toXDelta, float fromYDelta, float toYDelta, float cycles, long durationMillis, final boolean isBanClick) { TranslateAnimation translateAnimation = new TranslateAnimation( fromXDelta, toXDelta, fromYDelta, toYDelta); translateAnimation.setDuration(durationMillis); if (cycles > 0.0) { translateAnimation.setInterpolator(new CycleInterpolator(cycles)); } translateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if (isBanClick) { view.setClickable(false); } } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (isBanClick) { view.setClickable(true); } } }); view.startAnimation(translateAnimation); } /** * ???? * * @param view ??????? * @param fromXDelta X??????? * @param toXDelta X????????? * @param fromYDelta Y??????? * @param toYDelta Y????????? * @param cycles ???? * @param durationMillis ????? */ public static void translate(final View view, float fromXDelta, float toXDelta, float fromYDelta, float toYDelta, float cycles, long durationMillis) { translate(view, fromXDelta, toXDelta, fromYDelta, toYDelta, cycles, durationMillis, false); } /** * ???? * * @param view ??????? * @param fromXDelta X??????? * @param toXDelta X????????? * @param cycles ?????? * @param durationMillis ????? * @param isBanClick ?????????????????? */ public static void shake(View view, float fromXDelta, float toXDelta, float cycles, long durationMillis, final boolean isBanClick) { translate(view, fromXDelta, toXDelta, 0.0f, 0.0f, cycles, durationMillis, isBanClick); } /** * ???? * * @param view ??????? * @param fromXDelta X??????? * @param toXDelta X????????? * @param cycles ?????? * @param durationMillis ????? */ public static void shake(View view, float fromXDelta, float toXDelta, float cycles, long durationMillis) { translate(view, fromXDelta, toXDelta, 0.0f, 0.0f, cycles, durationMillis, false); } /** * ????????????10?????7? * * @param view * @param cycles ?????? * @param durationMillis ????? * @param isBanClick ?????????????????? */ public static void shake(View view, float cycles, long durationMillis, final boolean isBanClick) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, durationMillis, isBanClick); } /** * ????????????10????700?? * * @param view * @param cycles ?????? * @param isBanClick ?????????????????? */ public static void shake(View view, float cycles, final boolean isBanClick) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, 700, isBanClick); } /** * ????????????10 * * @param view * @param cycles ?????? * @param durationMillis ????? */ public static void shake(View view, float cycles, long durationMillis) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, durationMillis, false); } /** * ????????????10?????7? * * @param view * @param durationMillis ????? * @param isBanClick ?????????????????? */ public static void shake(View view, long durationMillis, final boolean isBanClick) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, durationMillis, isBanClick); } /** * ????????????10????700?? * * @param view ??????? * @param cycles ?????? */ public static void shake(View view, float cycles) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, 700, false); } /** * ????????????10?????7? * * @param view * @param durationMillis ????? */ public static void shake(View view, long durationMillis) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, durationMillis, false); } /** * ????????????10?????7?????700?? * * @param view * @param isBanClick ?????????????????? */ public static void shake(View view, final boolean isBanClick) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, 700, isBanClick); } /** * ????????????10?????7?????700?? * * @param view */ public static void shake(View view) { translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, 700, false); } }