From 486c229ea92cf1491c6563660d2b6b3bd1310637 Mon Sep 17 00:00:00 2001 From: Jaisel Rahman Date: Fri, 19 Apr 2019 19:11:01 +0530 Subject: [PATCH] Customize animation duration --- .../compactcalendarview/AnimationHandler.java | 29 +++++++++++++++---- .../CompactCalendarController.java | 12 ++++++++ .../CompactCalendarView.java | 8 +++++ library/src/main/res/values/attrs.xml | 2 ++ .../github/com/sample/CompactCalendarTab.java | 3 ++ sample/src/main/res/layout/main_tab.xml | 2 ++ 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/github/sundeepk/compactcalendarview/AnimationHandler.java b/library/src/main/java/com/github/sundeepk/compactcalendarview/AnimationHandler.java index cda5a1e0..6e0a59bd 100644 --- a/library/src/main/java/com/github/sundeepk/compactcalendarview/AnimationHandler.java +++ b/library/src/main/java/com/github/sundeepk/compactcalendarview/AnimationHandler.java @@ -10,8 +10,11 @@ class AnimationHandler { - private static final int HEIGHT_ANIM_DURATION_MILLIS = 650; - private static final int INDICATOR_ANIM_DURATION_MILLIS = 600; + private static final int DEFAULT_HEIGHT_ANIM_DURATION_MILLIS = 650; + private static final int DEFAULT_INDICATOR_ANIM_DURATION_MILLIS = 600; + + private int heightAnimDurationMillis = DEFAULT_HEIGHT_ANIM_DURATION_MILLIS; + private int indicatorAnimDurationMillis = DEFAULT_INDICATOR_ANIM_DURATION_MILLIS; private boolean isAnimating = false; private CompactCalendarController compactCalendarController; private CompactCalendarView compactCalendarView; @@ -20,19 +23,33 @@ class AnimationHandler { AnimationHandler(CompactCalendarController compactCalendarController, CompactCalendarView compactCalendarView) { this.compactCalendarController = compactCalendarController; this.compactCalendarView = compactCalendarView; + if (compactCalendarController.getHeightAnimDurationMillis() > -1) { + heightAnimDurationMillis = compactCalendarController.getHeightAnimDurationMillis(); + } + if (compactCalendarController.getIndicatorAnimDurationMillis() > -1) { + indicatorAnimDurationMillis = compactCalendarController.getIndicatorAnimDurationMillis(); + } } void setCompactCalendarAnimationListener(CompactCalendarView.CompactCalendarAnimationListener compactCalendarAnimationListener){ this.compactCalendarAnimationListener = compactCalendarAnimationListener; } + void setHeightAnimDuration(int durationMillis) { + this.heightAnimDurationMillis = durationMillis; + } + + void setIndicatorAnimDuration(int durationMillis) { + this.indicatorAnimDurationMillis = durationMillis; + } + void openCalendar() { if (isAnimating) { return; } isAnimating = true; Animation heightAnim = getCollapsingAnimation(true); - heightAnim.setDuration(HEIGHT_ANIM_DURATION_MILLIS); + heightAnim.setDuration(heightAnimDurationMillis); heightAnim.setInterpolator(new AccelerateDecelerateInterpolator()); compactCalendarController.setAnimationStatus(CompactCalendarController.EXPAND_COLLAPSE_CALENDAR); setUpAnimationLisForOpen(heightAnim); @@ -47,7 +64,7 @@ void closeCalendar() { } isAnimating = true; Animation heightAnim = getCollapsingAnimation(false); - heightAnim.setDuration(HEIGHT_ANIM_DURATION_MILLIS); + heightAnim.setDuration(heightAnimDurationMillis); heightAnim.setInterpolator(new AccelerateDecelerateInterpolator()); setUpAnimationLisForClose(heightAnim); compactCalendarController.setAnimationStatus(CompactCalendarController.EXPAND_COLLAPSE_CALENDAR); @@ -139,7 +156,7 @@ public void onAnimationEnd(Animator animation) { @NonNull private Animation getExposeCollapsingAnimation(final boolean isCollapsing) { Animation heightAnim = getCollapsingAnimation(isCollapsing); - heightAnim.setDuration(HEIGHT_ANIM_DURATION_MILLIS); + heightAnim.setDuration(heightAnimDurationMillis); heightAnim.setInterpolator(new AccelerateDecelerateInterpolator()); return heightAnim; } @@ -152,7 +169,7 @@ private Animation getCollapsingAnimation(boolean isCollapsing) { @NonNull private Animator getIndicatorAnimator(float from, float to) { ValueAnimator animIndicator = ValueAnimator.ofFloat(from, to); - animIndicator.setDuration(INDICATOR_ANIM_DURATION_MILLIS); + animIndicator.setDuration(indicatorAnimDurationMillis); animIndicator.setInterpolator(new OvershootInterpolator()); animIndicator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override diff --git a/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarController.java b/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarController.java index af565937..fccdbc3b 100755 --- a/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarController.java +++ b/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarController.java @@ -70,6 +70,8 @@ class CompactCalendarController { private float growfactorIndicator; private float distanceX; private long lastAutoScrollFromFling; + private int heightAnimDurationMillis; + private int indicatorAnimDurationMillis; private boolean useThreeLetterAbbreviation = false; private boolean isSmoothScrolling; @@ -160,6 +162,8 @@ private void loadAttributes(AttributeSet attrs, Context context) { currentSelectedDayIndicatorStyle = typedArray.getInt(R.styleable.CompactCalendarView_compactCalendarCurrentSelectedDayIndicatorStyle, FILL_LARGE_INDICATOR); displayOtherMonthDays = typedArray.getBoolean(R.styleable.CompactCalendarView_compactCalendarDisplayOtherMonthDays, displayOtherMonthDays); shouldSelectFirstDayOfMonthOnScroll = typedArray.getBoolean(R.styleable.CompactCalendarView_compactCalendarShouldSelectFirstDayOfMonthOnScroll, shouldSelectFirstDayOfMonthOnScroll); + heightAnimDurationMillis = typedArray.getInteger(R.styleable.CompactCalendarView_compactCalendarHeightAnimDuration, -1); + indicatorAnimDurationMillis = typedArray.getInteger(R.styleable.CompactCalendarView_compactCalendarIndicatorAnimDuration, -1); } finally { typedArray.recycle(); } @@ -293,6 +297,14 @@ int getTargetHeight() { return targetHeight; } + int getHeightAnimDurationMillis() { + return heightAnimDurationMillis; + } + + int getIndicatorAnimDurationMillis() { + return indicatorAnimDurationMillis; + } + int getWidth(){ return width; } diff --git a/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarView.java b/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarView.java index 904c5bcd..c7c6a2f7 100755 --- a/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarView.java +++ b/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarView.java @@ -103,6 +103,14 @@ public void setAnimationListener(CompactCalendarAnimationListener compactCalenda animationHandler.setCompactCalendarAnimationListener(compactCalendarAnimationListener); } + public void setHeightAnimDuration(int durationMillis) { + animationHandler.setHeightAnimDuration(durationMillis); + } + + public void setIndicatorAnimDuration(int durationMillis) { + animationHandler.setIndicatorAnimDuration(durationMillis); + } + /* Use a custom locale for compact calendar and reinitialise the view. */ diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index be80746e..63366d3b 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -29,5 +29,7 @@ + + diff --git a/sample/src/main/java/sundeepk/github/com/sample/CompactCalendarTab.java b/sample/src/main/java/sundeepk/github/com/sample/CompactCalendarTab.java index 7d1b44a1..b2f76d71 100644 --- a/sample/src/main/java/sundeepk/github/com/sample/CompactCalendarTab.java +++ b/sample/src/main/java/sundeepk/github/com/sample/CompactCalendarTab.java @@ -86,6 +86,9 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, // show Sunday as first day of month // compactCalendarView.setShouldShowMondayAsFirstDay(false); + // set animation duration + // compactCalendarView.setHeightAnimDuration(300); + //set initial title toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar(); toolbar.setTitle(dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth())); diff --git a/sample/src/main/res/layout/main_tab.xml b/sample/src/main/res/layout/main_tab.xml index 0ea6a7dd..72d4b327 100644 --- a/sample/src/main/res/layout/main_tab.xml +++ b/sample/src/main/res/layout/main_tab.xml @@ -22,6 +22,8 @@ app:compactCalendarEventIndicatorStyle="small_indicator" app:compactCalendarOtherMonthDaysTextColor="#534c4c" app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true" + app:compactCalendarHeightAnimDuration="300" + app:compactCalendarIndicatorAnimDuration="300" />