Aide学

Android linearlayout中view的显示/隐藏动

2017-05-22  本文已影响0人  _祥_1990

效果图:


a.gif

代码:

动画工具类

<pre>
package test.j.com.test.utils;

import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;

/**

public class AnimationUtils {
/**
* 将view从不可见变为可见的动画,原理:动态改变其LayoutParams.height的值
* @param view 要展示动画的view
*/
public static void visibleAnimator(final View view){
if(view!=null) {
int viewHeight=view.getHeight();
if(viewHeight==0){
int width=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(width,height);
viewHeight=view.getMeasuredHeight();
}
ValueAnimator animator=ValueAnimator.ofInt(0,viewHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params=view.getLayoutParams();
params.height= (int) animation.getAnimatedValue();
view.setLayoutParams(params);
}
});
animator.start();
}
}

/**
 * 将view从可见变为不可见的动画,原理:动态改变其LayoutParams.height的值
 * @param view 要展示动画的view
 */
public static void invisibleAnimator(final View view){
    if(view!=null){
        int viewHeight=view.getHeight();
        if(viewHeight==0){
            int width=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            int height=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            view.measure(width,height);
            viewHeight=view.getMeasuredHeight();
        }
        ValueAnimator animator=ValueAnimator.ofInt(viewHeight,0);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ViewGroup.LayoutParams params=view.getLayoutParams();
                params.height= (int) animation.getAnimatedValue();
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }
}

/**
 * 动态改变view的高度动画效果,动画时长300毫秒[android属性动画默认时长]
 * 原理:动画改变view LayoutParams.height的值
 * @param view 要进行高度改变动画的view
 * @param startHeight 动画前的view的高度
 * @param endHeight 动画后的view的高度
 */
public static void changeViewHeightAnimatorStart(final View view,final int startHeight,final int endHeight){
    if(view!=null&&startHeight>=0&&endHeight>=0){
        ValueAnimator animator=ValueAnimator.ofInt(startHeight,endHeight);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ViewGroup.LayoutParams params=view.getLayoutParams();
                params.height= (int) animation.getAnimatedValue();
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }
}

}
</pre>

Activity代码

<pre>
package test.j.com.test.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import test.j.com.test.R;
import test.j.com.test.utils.AnimationUtils;

/**

public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener{
Button btControlLayout;
Button btControlView;
LinearLayout llContainer;
TextView tvHeight0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
btControlLayout = (Button) findViewById(R.id.bt_control_layout);
btControlLayout.setTag(0);
btControlView = (Button) findViewById(R.id.bt_control_view);
btControlView.setTag(0);
llContainer= (LinearLayout) findViewById(R.id.ll_container);
tvHeight0= (TextView) findViewById(R.id.tv_height_0);
btControlLayout.setOnClickListener(this);
btControlView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.bt_control_layout:
            if(((int)(btControlLayout.getTag()))==0){
                btControlLayout.setTag(1);
                btControlLayout.setText("收起布局");
                AnimationUtils.visibleAnimator(llContainer);
            }else{
                btControlLayout.setTag(0);
                btControlLayout.setText("展开布局");
                AnimationUtils.invisibleAnimator(llContainer);
            }
            break;
        case R.id.bt_control_view:
            if(((int)(btControlView.getTag()))==0){
                btControlView.setTag(1);
                btControlView.setText("隐藏控件");
                AnimationUtils.changeViewHeightAnimatorStart(tvHeight0,
                        0,
                        (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150,
                                getResources().getDisplayMetrics()));
            }else{
                btControlView.setTag(0);
                btControlView.setText("显示控件");
                AnimationUtils.changeViewHeightAnimatorStart(tvHeight0,
                        (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150,
                                getResources().getDisplayMetrics()),
                0);
            }
            break;
    }
}

}

</pre>

布局文件

<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">

    <Button
        android:id="@+id/bt_control_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="展开布局"/>
    <Button
        android:id="@+id/bt_control_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示控件"/>
</LinearLayout>
<LinearLayout
    android:id="@+id/ll_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:divider="@color/colorPrimary"
    android:showDividers="middle"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="固定高度文本,高度50dp,居中显示"
        android:textColor="@color/colorPrimaryDark"
        android:gravity="center"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:text="固定高度文本,高度150dp,居中显示"
        android:textColor="@color/colorPrimaryDark"
        android:gravity="center"/>
</LinearLayout>
<TextView
    android:id="@+id/tv_height_0"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="100dp"
    android:text="初始高度为0的文本初始高度为0的文本初始高度为0的文本初始高度为0的文本初始高度为0的文本初始高度为0的文本初始高度为0的文本初始高度为0的文本初始高度为0的文本"
    android:textColor="@color/colorAccent"/>

</LinearLayout>
</pre>

上一篇 下一篇

猜你喜欢

热点阅读