dor*_*506 44 layout animation android android-animation
我想在Activity中调整一些布局的大小.
这是主XML的代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#3ee3e3" >
</LinearLayout>
<LinearLayout
android:id="@+id/middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#fe51e6" >
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如您所见,顶部和底部布局高度为0,中间布局覆盖所有位置.
我想以编程方式减少中间布局大小,同时增加顶部和底部布局大小,直到所有布局具有相同的高度.
我希望它看起来像一个动画.
我该怎么办?
谢谢
fay*_*lon 96
我为了类似的目的写了一个ResizeAnimation.它很简单但成本很高.
/**
* an animation for resizing the view.
*/
public class ResizeAnimation extends Animation {
private View mView;
private float mToHeight;
private float mFromHeight;
private float mToWidth;
private float mFromWidth;
public ResizeAnimation(View v, float fromWidth, float fromHeight, float toWidth, float toHeight) {
mToHeight = toHeight;
mToWidth = toWidth;
mFromHeight = fromHeight;
mFromWidth = fromWidth;
mView = v;
setDuration(300);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float height =
(mToHeight - mFromHeight) * interpolatedTime + mFromHeight;
float width = (mToWidth - mFromWidth) * interpolatedTime + mFromWidth;
LayoutParams p = mView.getLayoutParams();
p.height = (int) height;
p.width = (int) width;
mView.requestLayout();
}
}
Run Code Online (Sandbox Code Playgroud)
在 Honeycomb (Android 3.0) 上,有Animator和ObjectAnimator类可以实现更流畅的动画。
在这里阅读
有关如何使用弹跳插值器对视图组 (LinearLayout) 的移动进行动画处理的示例。
BounceInterpolator bounceInterpolator = new BounceInterpolator();
ObjectAnimator anim = ObjectAnimator.ofFloat(myViewGroup, "translationY", 0f, -200 );
anim.setInterpolator(bounceInterpolator);
anim.setDuration(1100).start();
Run Code Online (Sandbox Code Playgroud)
这将触发具有弹跳效果的平滑动画,并且真正移动视图,而不是像 Honeycomb 之前的动画一样。来自文档:
以前的动画改变了目标对象的视觉外观......但它们实际上并没有改变对象本身。