如何在 Android 中为 LayoutParams x 和 y 设置动画
How to animate LayoutParams x and y in Android
我是 android 的新手。
我有以下代码:
// layoutParams of type WindowManager.LayoutParams
layoutParams.x = someX;
layoutParams.y = someY;
windowManager.updateViewLayout(someView, layoutParams);
如何使这种变化动起来?翻译之类的东西...
您可以使用 ValueAnimator
:
int beginValue = 0;
int endValue = 100;
ValueAnimator animator = ValueAnimator.ofInt(beginValue, endValue);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
WindowManager.LayoutParams layoutParams =
(WindowManager.LayoutParams) view.getLayoutParams();
layoutParams.x = (Integer) animation.getAnimatedValue();
layoutParams.y = (Integer) animation.getAnimatedValue();
getWindowManager().updateViewLayout(view, params);
}
});
animator.start();
在 x 和 y 上设置动画时,最好使用 属性 Value Holders。
public void animate(final View v, int startX, int endX, int startY, int endY) {
PropertyValuesHolder pvhX = PropertyValuesHolder.ofInt("x", startX, endX);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofInt("y", startY, endY);
ValueAnimator translator = ValueAnimator.ofPropertyValuesHolder(pvhX, pvhY);
translator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) v.getLayoutParams();
layoutParams.x = (Integer) valueAnimator.getAnimatedValue("x");
layoutParams.y = (Integer) valueAnimator.getAnimatedValue("y");
windowManager.updateViewLayout(v, layoutParams);
}
});
translator.setDuration(100);
translator.start();
}
我发现的类似问题的最简单解决方案如下所示:https://www.youtube.com/watch?v=55wLsaWpQ4g
在我的例子中,我有一个 RelativeLayout
并且想以编程方式更改它的 android:layout_below
值(它的父级也是 RelativeLayout
)。
所以在 onCreate
我输入了:
LayoutTransition layoutTransition = rlListLayout.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
然后例如在 onClick
方法中更改了布局参数:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.dummy);
rl.setLayoutParams(params);
就是这样。 5行代码,无需计算位置,也无需测量任何东西。
我是 android 的新手。 我有以下代码:
// layoutParams of type WindowManager.LayoutParams
layoutParams.x = someX;
layoutParams.y = someY;
windowManager.updateViewLayout(someView, layoutParams);
如何使这种变化动起来?翻译之类的东西...
您可以使用 ValueAnimator
:
int beginValue = 0;
int endValue = 100;
ValueAnimator animator = ValueAnimator.ofInt(beginValue, endValue);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
WindowManager.LayoutParams layoutParams =
(WindowManager.LayoutParams) view.getLayoutParams();
layoutParams.x = (Integer) animation.getAnimatedValue();
layoutParams.y = (Integer) animation.getAnimatedValue();
getWindowManager().updateViewLayout(view, params);
}
});
animator.start();
在 x 和 y 上设置动画时,最好使用 属性 Value Holders。
public void animate(final View v, int startX, int endX, int startY, int endY) {
PropertyValuesHolder pvhX = PropertyValuesHolder.ofInt("x", startX, endX);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofInt("y", startY, endY);
ValueAnimator translator = ValueAnimator.ofPropertyValuesHolder(pvhX, pvhY);
translator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) v.getLayoutParams();
layoutParams.x = (Integer) valueAnimator.getAnimatedValue("x");
layoutParams.y = (Integer) valueAnimator.getAnimatedValue("y");
windowManager.updateViewLayout(v, layoutParams);
}
});
translator.setDuration(100);
translator.start();
}
我发现的类似问题的最简单解决方案如下所示:https://www.youtube.com/watch?v=55wLsaWpQ4g
在我的例子中,我有一个 RelativeLayout
并且想以编程方式更改它的 android:layout_below
值(它的父级也是 RelativeLayout
)。
所以在 onCreate
我输入了:
LayoutTransition layoutTransition = rlListLayout.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
然后例如在 onClick
方法中更改了布局参数:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.dummy);
rl.setLayoutParams(params);
就是这样。 5行代码,无需计算位置,也无需测量任何东西。