Created
August 24, 2018 12:25
-
-
Save clxy/7d4d4b155284b6e444623ddf1a88844f to your computer and use it in GitHub Desktop.
Android Databinding
This file contains hidden or 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
public class BindingUtil { | |
/** | |
* 四方形是基准。none:不做任何调整,width:基于宽度调整成四方形 | |
*/ | |
public enum Size { | |
none, width, height | |
} | |
/** | |
* 和Picasso不合。Picasso拿不到新的Layout尺寸。 | |
* @param view | |
* @param by | |
*/ | |
@BindingAdapter("android:squareBy") | |
public static void bindSquare(final View view, final Size by) { | |
if (by == Size.none) return; | |
view.getViewTreeObserver().addOnGlobalLayoutListener( | |
new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
int size = by == Size.height ? view.getHeight() : view.getWidth(); | |
ViewGroup.LayoutParams params = view.getLayoutParams(); | |
params.width = size; | |
params.height = size; | |
view.setLayoutParams(params); | |
view.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
} | |
} | |
); | |
} | |
/** | |
* 和Picasso不合。Picasso拿不到新的Layout尺寸。 | |
* @param view | |
* @param by | |
*/ | |
@BindingAdapter("android:squareBy") | |
public static void bindSquare(final View view, final int by) { | |
bindSquare(view, Size.values()[by]); | |
} | |
private BindingUtil() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment