以 Nine-Patch 为背景在视图中展开动画
Expanding animation on view with Nine-Patch as background
我正在尝试创建一个从上到下展开 View
的动画。
像这样:
我不能在 pivotY
设置为 0 的情况下使用 ScaleAnimation
,因为这会像普通 Bitmap
一样拉伸 Nine-Patch。我需要修改我的 View
.
的高度
我目前的解决方案:(test
是View参考)
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();
这非常有效,但它向两侧扩展了视野:
有谁知道制作这种动画的方法吗? 感谢任何帮助。
一个解决方案是保留您的实现,但通过在上方添加一个不会让您的视图增大的元素来限制扩展到底部:
示例:
<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,并且在您希望展开的视图上方,因此它不会展开高度,因为它不能与同一层次结构的元素重叠, 所以它会向下扩展
如果您通过将 android:below="@id/framelayout_id"
添加到 YourView 来使用 RelativeLayout,这也可以保证 YourView 不会向上扩展
添加一个额外的变量来保存 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();
}
我正在尝试创建一个从上到下展开 View
的动画。
像这样:
我不能在 pivotY
设置为 0 的情况下使用 ScaleAnimation
,因为这会像普通 Bitmap
一样拉伸 Nine-Patch。我需要修改我的 View
.
我目前的解决方案:(test
是View参考)
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();
这非常有效,但它向两侧扩展了视野:
有谁知道制作这种动画的方法吗? 感谢任何帮助。
一个解决方案是保留您的实现,但通过在上方添加一个不会让您的视图增大的元素来限制扩展到底部:
示例:
<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,并且在您希望展开的视图上方,因此它不会展开高度,因为它不能与同一层次结构的元素重叠, 所以它会向下扩展
如果您通过将 android:below="@id/framelayout_id"
添加到 YourView 来使用 RelativeLayout,这也可以保证 YourView 不会向上扩展
添加一个额外的变量来保存 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();
}