-
-
Save shibbirweb/e8de5133fc88cb8b5d92aef082dd92ef to your computer and use it in GitHub Desktop.
[android] Animate margins with ObjectAnimator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View; | |
import android.view.ViewGroup.MarginLayoutParams; | |
/** | |
* Allows an ObjectAnimator to set/get margins of a view | |
*/ | |
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import android.view.ViewGroup | |
class MarginProxy(private val view: View) { | |
companion object{ | |
const val LEFT = "left" | |
const val TOP = "top" | |
const val RIGHT = "right" | |
const val BOTTOM = "bottom" | |
} | |
private val layoutParams by lazy { | |
view.layoutParams as ViewGroup.MarginLayoutParams | |
} | |
fun getMargin(side: String): Int { | |
return when (side){ | |
LEFT -> layoutParams.leftMargin | |
TOP -> layoutParams.topMargin | |
RIGHT -> layoutParams.rightMargin | |
else -> layoutParams.bottomMargin | |
} | |
} | |
fun setMargin(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) { | |
val leftMargin = left ?: layoutParams.leftMargin | |
val topMargin = top ?: layoutParams.topMargin | |
val rightMargin = right ?: layoutParams.rightMargin | |
val bottomMargin = bottom ?: layoutParams.bottomMargin | |
layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin) | |
view.requestLayout() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment