From dce33fe2b42cde8f2cf4ae5ddd5e27b4edc049f5 Mon Sep 17 00:00:00 2001 From: Negue Date: Sat, 27 Jun 2015 20:23:14 +0200 Subject: [PATCH] Animate Value Bar --- Habitica/res/layout/value_bar.xml | 4 +- .../habitica/ui/AvatarWithBarsViewModel.java | 40 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Habitica/res/layout/value_bar.xml b/Habitica/res/layout/value_bar.xml index 3835bad6f..304450091 100644 --- a/Habitica/res/layout/value_bar.xml +++ b/Habitica/res/layout/value_bar.xml @@ -49,14 +49,14 @@ android:layout_margin="2dip" android:layout_weight="1" android:background="@{barForegroundColor}" - app:layout_weight="@{weightToShow}" /> + app:layout_weight_anim="@{weightToShow}" /> + app:layout_weight_anim="@{weightToHide}" /> diff --git a/Habitica/src/com/habitrpg/android/habitica/ui/AvatarWithBarsViewModel.java b/Habitica/src/com/habitrpg/android/habitica/ui/AvatarWithBarsViewModel.java index 04c406cfc..72c327ac8 100644 --- a/Habitica/src/com/habitrpg/android/habitica/ui/AvatarWithBarsViewModel.java +++ b/Habitica/src/com/habitrpg/android/habitica/ui/AvatarWithBarsViewModel.java @@ -5,6 +5,8 @@ import android.databinding.BindingAdapter; import android.databinding.DataBindingUtil; import android.util.Log; import android.view.View; +import android.view.animation.Animation; +import android.view.animation.Transformation; import android.widget.ImageView; import android.widget.LinearLayout; @@ -77,7 +79,7 @@ public class AvatarWithBarsViewModel { // but on a 1.0/0.0 which switches to 0.0/1.0 it shows the blank part full size... private void SetValueBar(ValueBarBinding valueBar, float value, float valueMax, String postString, int color, int colorBackground, int textColor) { - double percent = value / valueMax; + double percent = Math.min(1, value / valueMax); if(percent == 1) { @@ -106,4 +108,40 @@ public class AvatarWithBarsViewModel { view.setLayoutParams(layout); } + + @BindingAdapter("app:layout_weight_anim") + public static void setLayoutWeightAnim(View view, float weight) { + LayoutWeightAnimation anim = new LayoutWeightAnimation(view, weight); + anim.setDuration(1250); + + view.startAnimation(anim); + } + + public static class LayoutWeightAnimation extends Animation { + float targetWeight; + float initializeWeight; + View view; + + LinearLayout.LayoutParams layoutParams; + + public LayoutWeightAnimation(View view, float targetWeight) { + this.view = view; + this.targetWeight = targetWeight; + + layoutParams = (LinearLayout.LayoutParams)view.getLayoutParams(); + initializeWeight = layoutParams.weight; + } + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + layoutParams.weight = initializeWeight + (targetWeight - initializeWeight) * interpolatedTime; + + view.requestLayout(); + } + + @Override + public boolean willChangeBounds() { + return true; + } + } }