如何在安卓中实现布局动画?

4
在我的Activity屏幕中,有一半的屏幕包含一个布局。当Activity加载时,它是可见的,10秒后它会慢慢下降,最终对用户不可见。但它会慢慢下降。我该怎么做呢?请问有人可以帮助我吗?
提前感谢。

你是在控制视图的显示和隐藏吗?你想要展示垂直缩放动画吗?你的代码在哪里? - TheFlash
4个回答

22
res\anim文件夹中(如果没有该文件夹,请创建)创建slide_out_down.xml文件,并粘贴以下内容。
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"

android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="@android:integer/config_longAnimTime" />

要开始动画并隐藏视图,请使用此命令

 private void hideView(final View view){
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_out_down);
    //use this to make it longer:  animation.setDuration(1000);
    animation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
             view.setVisibility(View.GONE);
        }
    });

    view.startAnimation(animation);
}

你能给我你的邮件地址吗,这样我可以通过屏幕截图来解释我的情况。 - user2466123
好的,那么只需要在标题和页脚之间添加一个FrameLayout,在其中放置您的第二个布局。这样,当您动画化您的第二个视图时,它将保持在您的标题和页脚之间。 - Plato
我已经尝试使用FrameLayout,但是布局向下移动到其下面的布局上。 - user2466123
你把第二个视图放在FrameLayout中并对其进行了动画处理? - Plato
和向下一样,我需要向上移动来保持同样的布局。你能告诉我怎么做吗? - user2466123
显示剩余5条评论

2
public void animateLayout(){
        LinearLayout layout = findViewById(R.id.layoutId);
        layout.animate().translationYBy(1000f).setDuration(50000);
    }

以上代码将使视图缓慢地变为不可见。

setDuration(50000)//根据您的需求更改数字。它会改变布局的速度。


1
你可以使用FragmentActivity和Fragment来实现此功能,并为片段添加动画。
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
 <scale
  android:fromXScale="1.0" android:toXScale="0.0"
  android:fromYScale="1.0" android:toYScale="0.0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="1000" 
 />


0
try this:

// gone layout
     collapse(recipientLayout);
//show layout
      expand(recipientLayout);

    public void expand(final LinearLayout v) {
    v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final int targtetHeight = v.getMeasuredHeight();
    /*if (v.isShown()) {
        collapse(v);
    } else */{
        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                                               Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1 ? LinearLayout.LayoutParams.WRAP_CONTENT
                        : (int) (targtetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        a.setDuration((int) (targtetHeight + 600));
        v.startAnimation(a);
    }

}

public void collapse(final LinearLayout v) {
    final int initialHeight = v.getMeasuredHeight();
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            /*if (v.isShown()) {
                collapse(v);
            }*/
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight
                        - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int) (v.getLayoutParams().height + 600));
    v.startAnimation(a);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接