Skip to content

Instantly share code, notes, and snippets.

@vmax
Created April 4, 2016 17:57
Show Gist options
  • Save vmax/d3acf0c789b009ec4367bb2cbaffde47 to your computer and use it in GitHub Desktop.
Save vmax/d3acf0c789b009ec4367bb2cbaffde47 to your computer and use it in GitHub Desktop.
A wrapper for animating changes of android:weight property of views inside LinearLayout using ObjectAnimator
public class ViewWeightAnimationWrapper {
private View view;
public ViewWeightAnimationWrapper(View view) {
if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
this.view = view;
} else {
throw new IllegalArgumentException("The view should have LinearLayout as parent");
}
}
public void setWeight(float weight) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.weight = weight;
view.setLayoutParams(params);
}
public float getWeight() {
return ((LinearLayout.LayoutParams) view.getLayoutParams()).weight;
}
}
// code sample
ObjectAnimator.ofFloat(new ViewWeightAnimationWrapper(view), "weight", oldWeight, newWeight).start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment