Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize animation duration #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -293,6 +297,14 @@ int getTargetHeight() {
return targetHeight;
}

int getHeightAnimDurationMillis() {
return heightAnimDurationMillis;
}

int getIndicatorAnimDurationMillis() {
return indicatorAnimDurationMillis;
}

int getWidth(){
return width;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@
<attr name="compactCalendarDisplayOtherMonthDays" format="boolean"/>
<attr name="compactCalendarShouldSelectFirstDayOfMonthOnScroll" format="boolean"/>
<attr name="compactCalendarOtherMonthDaysTextColor" format="color"/>
<attr name="compactCalendarHeightAnimDuration" format="integer"/>
<attr name="compactCalendarIndicatorAnimDuration" format="integer"/>
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/layout/main_tab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
app:compactCalendarEventIndicatorStyle="small_indicator"
app:compactCalendarOtherMonthDaysTextColor="#534c4c"
app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true"
app:compactCalendarHeightAnimDuration="300"
app:compactCalendarIndicatorAnimDuration="300"
/>

<Button
Expand Down