Skip to content

Commit

Permalink
feat(Poll): scale animation based on votes
Browse files Browse the repository at this point in the history
Updates the animation timing, to be based on the amount of votes a
option recieved relative to the other options. This means a option with
more votes will run longer than one with less votes. Overall this makes
the animation appear more dynamic and smoother.
  • Loading branch information
FineFindus committed May 19, 2024
1 parent 44e3e5f commit 9dda89f
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,19 @@ public void showResults(boolean shown) {
item.showResults = shown;
item.calculateResults();
Drawable bg=progressBg;
long animationDuration = (long) (ANIMATION_DURATION*item.votesFraction);
int startLevel=shown ? 0 : progressBg.getLevel();
int targetLevel=shown ? Math.round(10000f*item.votesFraction) : 0;
ObjectAnimator animator=ObjectAnimator.ofInt(bg, "level", startLevel, targetLevel);
animator.setDuration(ANIMATION_DURATION);
animator.setDuration(animationDuration);
animator.setInterpolator(new DecelerateInterpolator());
button.setBackground(bg);
if(shown){
itemView.setSelected(item.poll.ownVotes!=null && item.poll.ownVotes.contains(item.optionIndex));
// animate percent
percent.setVisibility(View.VISIBLE);
ValueAnimator percentAnimation=ValueAnimator.ofInt(0, Math.round(100f*item.votesFraction));
percentAnimation.setDuration(ANIMATION_DURATION);
percentAnimation.setDuration(animationDuration);
percentAnimation.setInterpolator(new DecelerateInterpolator());
percentAnimation.addUpdateListener(animation -> percent.setText(String.format(Locale.getDefault(), "%d%%", (int) animation.getAnimatedValue())));
percentAnimation.start();
Expand Down

0 comments on commit 9dda89f

Please sign in to comment.