如何在android中设置布局动画?

时间:2013-06-27 10:34:59

标签: android android-animation

在我的“活动”屏幕中,屏幕的一半包含一个布局。当“活动”加载时它可见,10秒后它将缓慢下降,最后它将对用户不可见。但是它慢慢下来。我该怎么做它。任何人都可以帮助我。

提前感谢。

4 个答案:

答案 0 :(得分:14)

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);
}

答案 1 :(得分: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" 
 />

答案 2 :(得分:1)

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

上面的代码会让视图变得非常缓慢。

setDuration(50000) //根据您的需要更改号码。它改变了布局的速度。

答案 3 :(得分: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);
}