Skip to content

Instantly share code, notes, and snippets.

@sdex
Forked from openback/MarginProxy.java
Last active March 27, 2016 12:29
Show Gist options
  • Save sdex/c4d8ee6f3e8a7b383a2e to your computer and use it in GitHub Desktop.
Save sdex/c4d8ee6f3e8a7b383a2e to your computer and use it in GitHub Desktop.
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
/**
* Allows an ObjectAnimator to set/get margins of a view
* how to use:
* MarginProxy marginProxy = new MarginProxy(noteContainer);
* ObjectAnimator marginAnimation = ObjectAnimator.ofInt(marginProxy, "topMargin", oldTopMargin, newTopMargin).setDuration(300);
*/
public class MarginProxy {
private View mView;
public MarginProxy(View view) {
mView = view;
}
public int getLeftMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.leftMargin;
}
public void setLeftMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(margin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
mView.requestLayout();
}
public int getTopMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.topMargin;
}
public void setTopMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, margin, lp.rightMargin, lp.bottomMargin);
mView.requestLayout();
}
public int getRightMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.rightMargin;
}
public void setRightMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, margin, lp.bottomMargin);
mView.requestLayout();
}
public int getBottomMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.bottomMargin;
}
public void setBottomMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, margin);
mView.requestLayout();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment