1

我有一个LinearLayout( LayoutContentView) 可以包含一个或两个视图 ( SubView1and SubView2) (它们是TextViewor ImageView)。这layout是在另一个LinearLayout( MyScreenLayout) 中。

我想制作一个动画,LayoutContentView以便它可以移动并仅显示其中的一部分MyScreenLayout

这两种布局都有setClipChildren(false);,所以它可以让它的孩子在它自己之外画画。

根据不同的参数,我可以改变内容的大小,以及我将显示的内容的大小。

基本上,我从上到下展开以显示两个子视图,从下到上展开以仅显示第二个子视图。在展开之前,我增加了 的大小LayoutContentView,所以它可以显示两个子视图,在我展开之后,我减小了 的大小LayoutContentView,所以它只能显示第二个子视图,并在屏幕上为其他元素留出空间.

这是我的支出和不支出方法LayoutContentView

mLayoutContentView.clearAnimation();

float yFrom = 0.0F;
float yTo = 0.0F;
float xFrom = 0.0F;
float xTo = 0.0F;

if (expend) { // if we expend
    // I change the height of my LayoutContentView so it we can show it's two subviews
    final android.view.ViewGroup.LayoutParams lp = mLayoutContentView.getLayoutParams();
    lp.height = subView1H + subView2H;
    setLayoutParams(lp);
    invalidate(); 

    // we start the animation so it shows only the second subview
    yFrom = -subView1H;
    // and we animate from top to bottom until it shows the two subviews
    yTo = 0;
} else { // if we un-expend
    // we animate from bottom to top starting by showing the two subviews
    yFrom = 0;
    // and progressively hiding the first subview and showing only the second subview
    yTo = -subView1H;
}

Animation anim = new TranslateAnimation(xFrom, xTo, yFrom, yTo);
anim.setDuration(1000);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if (!expend) {
            // if we un expend at the end of the animation we can set the size of LayoutContentView to the size of the second subview again
            final android.view.ViewGroup.LayoutParams lp = mLayoutContentView.getLayoutParams();
            lp.height = subView2H;
            mLayoutContentView.setLayoutParams(lp);
            invalidate();
        }
    }
});

mLayoutContentView.startAnimation(anim);

我制作动画的方式需要它应用在LayoutContentView包含两个子视图的布局上,并且startAnimation()它不会做动画。我尝试使用 a LayoutAnimationController,但不是在 上做动画LayoutContentView,而是在它的每个孩子身上做动画......我也尝试自己对每个孩子做动画,但我不知道为什么,第二个子视图是没有显示。

每次我尝试使用HierarchyViewer,但它确实看到了动画所做的更改。

有谁知道解决方案或遇到同样的问题并找到了一个好的解决方案?

编辑:好吧,如果您background为您设置颜色TextView并使用动画移动它们,则background移动但即使您已将填充后参数设置为动画,背景也会移回其原始位置或类似位置,因此当我为我的两个SubViews 设置了背景颜色时,其中一个SubView的背景隐藏了另一个的背景......而且如果在动画之后,其中一个SubView仍然在其布局之外,还有一个问题在动画过程中也是一个问题,所以我在这里也添加了一个限制。

4

0 回答 0