Animate Value Bar

This commit is contained in:
Negue 2015-06-27 20:23:14 +02:00
parent c57d2219ad
commit dce33fe2b4
2 changed files with 41 additions and 3 deletions

View file

@ -49,14 +49,14 @@
android:layout_margin="2dip" android:layout_margin="2dip"
android:layout_weight="1" android:layout_weight="1"
android:background="@{barForegroundColor}" android:background="@{barForegroundColor}"
app:layout_weight="@{weightToShow}" /> app:layout_weight_anim="@{weightToShow}" />
<View <View
android:layout_width="0dip" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0" android:layout_weight="0"
android:padding="2dip" android:padding="2dip"
app:layout_weight="@{weightToHide}" /> app:layout_weight_anim="@{weightToHide}" />
</LinearLayout> </LinearLayout>

View file

@ -5,6 +5,8 @@ import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil; import android.databinding.DataBindingUtil;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; 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... // 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) 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) if(percent == 1)
{ {
@ -106,4 +108,40 @@ public class AvatarWithBarsViewModel {
view.setLayoutParams(layout); 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;
}
}
} }