记录Android平铺展开效果属性动画

  业务需求需要做下面需求,转自:Android布局平铺展开效果的属性动画-博客频道-CSDN.NEThttp://blog.csdn.net/debbytang/article/details/68496728,这里是通过针对布局的gone和visible属性实现的

1.看下布局文件

  展开开关控件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="信息状态"/>
    <Button
        android:id="@+id/btShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好"/>
</LinearLayout>

展开的布局

<LinearLayout
    android:id="@+id/ll_show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/c122"
    android:orientation="vertical"
    android:visibility="gone">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全部"
        android:textColor="@color/c001"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数据1"
        android:textColor="@color/c001"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数据2"
        android:textColor="@color/c001"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数据3"
        android:textColor="@color/c001"/>
</LinearLayout>

往下移动的布局
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shap_background"
    android:orientation="vertical">

<</LinearLayout>

2.首先针对封装的代码进行分析                                                                                                                                                             

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
//平铺布局
public class HiddenUtils {
    /**
     * 伸展高度
     */
    private int mHeight;
    /**
     * 需要展开隐藏的布局,开关控件
     */
    private View hideView, down;
    /**
     * 旋转动画
     */
    private RotateAnimation animation;

    private HiddenUtils(Context context, View hideView, View down, int height) {
        this.hideView = hideView;
        this.down = down;
        //获取对象中的属性值
        float mDensity = context.getResources().getDisplayMetrics().density;
        //伸展高度
        mHeight = (int) (mDensity * height + 0.5);
    }

    /**
     * 构造器(可根据自己需要修改传参)
     *
     * @param context  上下文
     * @param hideView 需要隐藏或显示的布局view
     * @param down     按钮开关的view
     * @param height   布局展开的高度(根据实际需要传)
     */
    public static HiddenUtils newInstance(Context context, View hideView, View down, int height) {
        return new HiddenUtils(context, hideView, down, height);
    }

    /**
     * 开关
     */
    public void toggle() {
        startAnimation();
        if (View.VISIBLE == hideView.getVisibility()) {
            //布局隐藏
            closeAnimate(hideView);
        } else {
            //布局铺开
            openAnim(hideView);
        }
    }

    /**
     * 开关旋转动画
     */
    private void startAnimation() {
        if (View.VISIBLE == hideView.getVisibility()) {
            animation = new RotateAnimation(90, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        } else {
            animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        }
        //设置动画持续时间
        animation.setDuration(30);
        animation.setInterpolator(new LinearInterpolator());
        //设置反方向执行
        animation.setRepeatMode(Animation.REVERSE);
        //动画执行完后是否停留在执行完的状态
        animation.setFillAfter(true);
        down.startAnimation(animation);
    }

    //打开隐藏的布局
    private void openAnim(View v) {
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0, mHeight);
        animator.start();
    }
    //关闭布局
    private void closeAnimate(final View view) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }
    //ValueAnimator通过名字不难发现,是针对某个控件设置缩放
    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}

3.使用方式

@OnClick({R2.id.btShow})
    public void onClickView(View view) {
        int i = view.getId();
        if (i == R.id.btShow) {
            HiddenAnimUtils.newInstance(TestHiddenAnimActivity.this, mllShow, btShow, 77).toggle();
        }
    }
}

     第一个参数不用多说指对应Activity,第二个参数指需要打开的布局,第三个参数点击控件,最后一个打开布局的高度  ,.调用toggle()执行触发; 

缺点:打开的布局不能动态设置数据         

 

推荐一款免费vpn: https://blog.csdn.net/qq_30944187/article/details/82191024

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值