我正在尝试创建一个从上到下展开View
的动画。
我无法将ScaleAnimation
与pivotY
设置为0,因为这会像正常Bitmap
一样拉伸Nine-Patch。我需要修改View
的高度。
我当前的解决方案:(test
是视图参考)
ValueAnimator anim = ValueAnimator.ofInt(test.getMeasuredHeight(), 800);
anim.setInterpolator(new FastOutSlowInInterpolator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = test.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
test.setLayoutParams(params);
}
});
anim.setDuration(250).start();
这很有效,但它扩展了双方的观点:
有谁知道创建此类动画的方法? 感谢任何帮助。
答案 0 :(得分:1)
一个解决方案是保留您的实施,但通过使一个元素高于不允许您的视图成长来限制扩展到底部:
示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
...
android:layout_height="100dp"/> //assuming your view starts 100dps from top
<YourView
...
.../>
</LinearLayout>
由于您的容器是一个LinearLayout,并且第一个View(FrameLayout)高度为100dp,并且在您希望展开的视图上方,因此它无法扩展它的高度,因为它不能重叠一个相同层次结构的元素,因此它将向下扩展
如果您通过向YourView添加android:below="@id/framelayout_id"
来使用RelativeLayout也可以使用,这也可以保证YourView不会扩展
答案 1 :(得分:0)
添加一个额外的变量,用于保存Y轴上的初始位置。
展开顶部
public void expandTop(final View test) {
final float bottomAnchor = test.getY() + test.getHeight();
final ValueAnimator anim = ValueAnimator.ofInt(test.getMeasuredHeight(), 800);
anim.setInterpolator(new FastOutSlowInInterpolator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = test.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
test.setY(bottomAnchor - (int) animation.getAnimatedValue());
test.setLayoutParams(params);
}
});
anim.setDuration(2500).start();
}
<强> expandBottom 强>
public void expandBottom(final View test) {
final float topAnchor = test.getY();
final ValueAnimator anim = ValueAnimator.ofInt(test.getMeasuredHeight(), 800);
anim.setInterpolator(new FastOutSlowInInterpolator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = test.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
test.setY(topAnchor);
test.setLayoutParams(params);
}
});
anim.setDuration(2500).start();
}