/** * 根据字符串,启动翻页公告 * * @param notice 字符串 * @param inAnimResId 进入动画的resID * @param outAnimResID 离开动画的resID */ @SuppressWarnings("deprecation") public void startWithText(final String notice, final @AnimRes int inAnimResId, final @AnimRes int outAnimResID) { if (TextUtils.isEmpty(notice)) return; getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { getViewTreeObserver().removeGlobalOnLayoutListener(this); } startWithFixedWidth(notice, inAnimResId, outAnimResID); } }); }
/** * 根据字符串和宽度,启动翻页公告 * * @param notice 字符串 */ private void startWithFixedWidth(String notice, @AnimRes int inAnimResId, @AnimRes int outAnimResID) { int noticeLength = notice.length(); int width = Utils.px2dip(getContext(), getWidth()); if (width == 0) { throw new RuntimeException("Please set the width of MarqueeView !"); } int limit = width / textSize; List list = new ArrayList(); if (noticeLength <= limit) { list.add(notice); } else { int size = noticeLength / limit + (noticeLength % limit != 0 ? 1 : 0); for (int i = 0; i < size; i++) { int startIndex = i * limit; int endIndex = ((i + 1) * limit >= noticeLength ? noticeLength : (i + 1) * limit); list.add(notice.substring(startIndex, endIndex)); } } if (notices == null) notices = new ArrayList<>(); notices.clear(); notices.addAll(list); start(inAnimResId, outAnimResID); }
/** * 结束到指定 Activity * * @param activity activity * @param isIncludeSelf 是否结束该 activity 自己 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static boolean finishToActivity(@NonNull final Activity activity, final boolean isIncludeSelf, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { List<Activity> activities = Utils.sActivityList; for (int i = activities.size() - 1; i >= 0; --i) { Activity aActivity = activities.get(i); if (aActivity.equals(activity)) { if (isIncludeSelf) { finishActivity(aActivity, enterAnim, exitAnim); } return true; } finishActivity(aActivity, enterAnim, exitAnim); } return false; }
/** * 结束到指定 Activity * * @param clz Activity 类 * @param isIncludeSelf 是否结束该 activity 自己 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static boolean finishToActivity(@NonNull final Class<?> clz, final boolean isIncludeSelf, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { List<Activity> activities = Utils.sActivityList; for (int i = activities.size() - 1; i >= 0; --i) { Activity aActivity = activities.get(i); if (aActivity.getClass().equals(clz)) { if (isIncludeSelf) { finishActivity(aActivity, enterAnim, exitAnim); } return true; } finishActivity(aActivity, enterAnim, exitAnim); } return false; }
/** * 结束所有其他类型的 Activity * * @param clz Activity 类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void finishOtherActivities(@NonNull final Class<?> clz, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { List<Activity> activities = Utils.sActivityList; boolean flag = false; for (int i = activities.size() - 1; i >= 0; i--) { Activity activity = activities.get(i); if (activity.getClass().equals(clz)) { if (flag) { finishActivity(activity, enterAnim, exitAnim); } else { flag = true; } } else { finishActivity(activity, enterAnim, exitAnim); } } }
/** * 设置进入动画和离开动画 * * @param inAnimResId 进入动画的resID * @param outAnimResID 离开动画的resID */ private void setInAndOutAnimation(@AnimRes int inAnimResId, @AnimRes int outAnimResID) { Animation inAnim = AnimationUtils.loadAnimation(getContext(), inAnimResId); if (hasSetAnimDuration) inAnim.setDuration(animDuration); setInAnimation(inAnim); Animation outAnim = AnimationUtils.loadAnimation(getContext(), outAnimResID); if (hasSetAnimDuration) outAnim.setDuration(animDuration); setOutAnimation(outAnim); }
/** * 启动Activity * * @param cls activity类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Class<?> cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(context, null, context.getPackageName(), cls.getName(), getOptionsBundle(context, enterAnim, exitAnim)); }
/** * 启动Activity * * @param activity activity * @param cls activity类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Activity activity, @NonNull final Class<?> cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, null, activity.getPackageName(), cls.getName(), getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动Activity * * @param extras extras * @param cls activity类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Class<?> cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(context, extras, context.getPackageName(), cls.getName(), getOptionsBundle(context, enterAnim, exitAnim)); }
/** * 启动Activity * * @param extras extras * @param activity activity * @param cls activity类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Activity activity, @NonNull final Class<?> cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, extras, activity.getPackageName(), cls.getName(), getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动Activity * * @param activity activity * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Activity activity, @NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, null, pkg, cls, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动Activity * * @param extras extras * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Activity activity, @NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, extras, pkg, cls, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动Activity * * @param activity activity * @param intent 意图 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Activity activity, @NonNull final Intent intent, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(intent, activity, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动Activity * * @param activity activity * @param intents 意图 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivities(@NonNull final Activity activity, @NonNull final Intent[] intents, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivities(intents, activity, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
@Override public XmlResourceParser getAnimation(@AnimRes int id) throws NotFoundException { int realId = getCorrespondResIdStrictly(id); if (realId > 0) { return mSkinResources.getAnimation(realId); } return super.getAnimation(id); }
public static void runLayoutAnimation(final RecyclerView recyclerView, @AnimRes int res) { final Context context = recyclerView.getContext(); final LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(context, res); recyclerView.setLayoutAnimation(controller); recyclerView.getAdapter().notifyDataSetChanged(); recyclerView.scheduleLayoutAnimation(); }
@Override public ExtraTransaction setCustomAnimations(@AnimRes int targetFragmentEnter , @AnimRes int currentFragmentPopExit) { mRecord.targetFragmentEnter = targetFragmentEnter; mRecord.currentFragmentPopExit = currentFragmentPopExit; mRecord.currentFragmentPopEnter = 0; mRecord.targetFragmentExit = 0; return this; }
@Override public ExtraTransaction setCustomAnimations(@AnimRes int targetFragmentEnter, @AnimRes int currentFragmentPopExit, @AnimRes int currentFragmentPopEnter, @AnimRes int targetFragmentExit) { mRecord.targetFragmentEnter = targetFragmentEnter; mRecord.currentFragmentPopExit = currentFragmentPopExit; mRecord.currentFragmentPopEnter = currentFragmentPopEnter; mRecord.targetFragmentExit = targetFragmentExit; return this; }
/** * Sets the exit animations. * * @param context Application context. * @param enterResId Resource ID of the "enter" animation for the application. * @param exitResId Resource ID of the "exit" animation for the browser. */ public Builder setExitAnimations( @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) { Bundle bundle = ActivityOptionsCompat.makeCustomAnimation( context, enterResId, exitResId).toBundle(); mIntent.putExtra(EXTRA_EXIT_ANIMATION_BUNDLE, bundle); return this; }
/** * Set a Custom Animation to a Fragment transaction * * @param transaction The transaction that will * @param enterAnim The animation resource to be used when the next fragment enters. * @param exitAnim The animation resource to be used when the current fragment exits. * @param popEnterAnim The animation resource to be used when the previous fragment enters on back pressed. * @param popExitAnim The animation resource to be used when the current fragment exits on back pressed.. */ private void setCustomAnimations(FragmentTransaction transaction, @AnimRes int enterAnim, @AnimRes int exitAnim, @AnimRes int popEnterAnim, @AnimRes int popExitAnim) { transaction.setCustomAnimations( enterAnim, exitAnim, popEnterAnim, popExitAnim ); }
/** * Set a Custom Animation to a Fragment transaction. * * @param enterAnim The animation resource to be used when the next fragment enters. * @param exitAnim The animation resource to be used when the current fragment exits. * @param popEnterAnim The animation resource to be used when the previous fragment enters on back pressed. * @param popExitAnim The animation resource to be used when the current fragment exits on back pressed.. */ public Builder setCustomTransactionAnimation(@AnimRes int enterAnim, @AnimRes int exitAnim, @AnimRes int popEnterAnim, @AnimRes int popExitAnim) { data.setEnterAnim(enterAnim); data.setExitAnim(exitAnim); data.setPopEnterAnim(popEnterAnim); data.setPopExitAnim(popExitAnim); return this; }
/** * 替换fragement 并添加动画 */ public void replaceFragment(@IdRes int id_content, @AnimRes int anim_in, @AnimRes int anim_out, Fragment fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // transaction.setCustomAnimations(anim_in, anim_out); transaction.replace(id_content, fragment); transaction.commit(); }
/** * 替换fragement 并添加动画 * 替换fragement的同时对fragment进行“入back栈”操作 ,据我所知并不是真正的进入内存栈 * * @param id_content * @param anim_in * @param anim_out * @param fragment */ @Deprecated public void replaceFragmentWithBackStack(@IdRes int id_content, @AnimRes int anim_in, @AnimRes int anim_out, Fragment fragment) { String tag = fragment.getClass().getSimpleName(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // transaction.setCustomAnimations(anim_in, anim_out); transaction.replace(id_content, fragment, tag); //记录commit一次操作,与mFragmentManager.popBackStack();匹配使用 transaction.addToBackStack(tag); //commitAllowingStateLoss 优于 commit 方法,当使用commit方法时,系统将进行状态判断,如果状态(mStateSaved)已经保存,将发生"Can not perform this action after onSaveInstanceState"错误。 //如果mNoTransactionsBecause已经存在,将发生"Can not perform this action inside of " + mNoTransactionsBecause错误。 transaction.commitAllowingStateLoss(); }
/** * 启动Activity * * @param extras extras * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Activity activity, @NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, extras, pkg, cls, null); activity.overridePendingTransition(enterAnim, exitAnim); }
/** * Method used to set the Animations when the Custom Tab opens * @param startEnterAnimation The Enter Animation of the custom tab * @param startCloseAnimation The Close Animation of the new activity */ public Style setStartAnimation(@AnimRes int startEnterAnimation, @AnimRes int startCloseAnimation){ this.startEnterAnimation = startEnterAnimation; this.startCloseAnimation = startCloseAnimation; return this; }
/** * Method used to set the Animations when the Custom Tab opens * @param exitEnterAnimation The Enter Animation of the new activity * @param exitCloseAnimation The Close Animation of the custom tab */ public Style setExitAnimation(@AnimRes int exitEnterAnimation, @AnimRes int exitCloseAnimation){ this.exitEnterAnimation = exitEnterAnimation; this.exitCloseAnimation = exitCloseAnimation; return this; }
@Override public ViewBlock setCustomAnimations(@AnimRes int enter, @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit) { mEnterAnim = enter; mExitAnim = exit; mPopEnterAnim = popEnter; mPopExitAnim = popExit; return this; }
/** * 替换 fragment * * @param srcFragment 源 fragment * @param destFragment 目标 fragment * @param isAddStack 是否入回退栈 * @param enterAnim 入场动画 * @param exitAnim 出场动画 * @param popEnterAnim 入栈动画 * @param popExitAnim 出栈动画 */ public static void replace(@NonNull final Fragment srcFragment, @NonNull final Fragment destFragment, final boolean isAddStack, @AnimRes final int enterAnim, @AnimRes final int exitAnim, @AnimRes final int popEnterAnim, @AnimRes final int popExitAnim) { Args args = getArgs(srcFragment); replace(srcFragment.getFragmentManager(), destFragment, args.id, isAddStack, enterAnim, exitAnim, popEnterAnim, popExitAnim); }
/** * 替换 fragment * * @param fm fragment 管理器 * @param containerId 布局 Id * @param fragment fragment * @param isAddStack 是否入回退栈 * @param enterAnim 入场动画 * @param exitAnim 出场动画 * @param popEnterAnim 入栈动画 * @param popExitAnim 出栈动画 */ public static void replace(@NonNull final FragmentManager fm, @NonNull final Fragment fragment, @IdRes final int containerId, final boolean isAddStack, @AnimRes final int enterAnim, @AnimRes final int exitAnim, @AnimRes final int popEnterAnim, @AnimRes final int popExitAnim) { FragmentTransaction ft = fm.beginTransaction(); putArgs(fragment, new Args(containerId, false, isAddStack)); addAnim(ft, enterAnim, exitAnim, popEnterAnim, popExitAnim); operate(TYPE_REPLACE_FRAGMENT, fm, ft, null, fragment); }
/** * 启动 Activity * * @param clz Activity 类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Class<?> clz, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(context, null, context.getPackageName(), clz.getName(), getOptionsBundle(context, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context instanceof Activity) { ((Activity) context).overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param activity activity * @param clz Activity 类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Activity activity, @NonNull final Class<?> clz, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, null, activity.getPackageName(), clz.getName(), getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param extras extras * @param clz Activity 类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Class<?> clz, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(context, extras, context.getPackageName(), clz.getName(), getOptionsBundle(context, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context instanceof Activity) { ((Activity) context).overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param extras extras * @param activity activity * @param clz Activity 类 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Activity activity, @NonNull final Class<?> clz, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, extras, activity.getPackageName(), clz.getName(), getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(context, null, pkg, cls, getOptionsBundle(context, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context instanceof Activity) { ((Activity) context).overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param activity activity * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Activity activity, @NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, null, pkg, cls, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param extras extras * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(context, extras, pkg, cls, getOptionsBundle(context, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context instanceof Activity) { ((Activity) context).overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param extras extras * @param pkg 包名 * @param cls 全类名 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Bundle extras, @NonNull final Activity activity, @NonNull final String pkg, @NonNull final String cls, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(activity, extras, pkg, cls, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param intent 意图 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Intent intent, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivity(intent, context, getOptionsBundle(context, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context instanceof Activity) { ((Activity) context).overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动 Activity * * @param activity activity * @param intent 意图 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivity(@NonNull final Activity activity, @NonNull final Intent intent, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { startActivity(intent, activity, getOptionsBundle(activity, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { activity.overridePendingTransition(enterAnim, exitAnim); } }
/** * 启动多个 Activity * * @param intents 意图 * @param enterAnim 入场动画 * @param exitAnim 出场动画 */ public static void startActivities(@NonNull final Intent[] intents, @AnimRes final int enterAnim, @AnimRes final int exitAnim) { Context context = getActivityOrApp(); startActivities(intents, context, getOptionsBundle(context, enterAnim, exitAnim)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN && context instanceof Activity) { ((Activity) context).overridePendingTransition(enterAnim, exitAnim); } }