值动画

  • 界面默认收缩

  • 界面展开

  • 实现如上图红线区域布局

<LinearLayout
        android:id="@+id/safe_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/safe_title_layout"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/des_layout_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/des_iv_1"
                android:layout_width="@dimen/des_iv_width"
                android:layout_height="@dimen/des_iv_height"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/des_tv_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="#ff7a7a7a"
                android:textSize="@dimen/des_tv_size" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/des_layout_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/des_iv_2"
                android:layout_width="@dimen/des_iv_width"
                android:layout_height="@dimen/des_iv_height"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/des_tv_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="#ff7a7a7a"
                android:textSize="@dimen/des_tv_size" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/des_layout_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/des_iv_3"
                android:layout_width="@dimen/des_iv_width"
                android:layout_height="@dimen/des_iv_height"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/des_tv_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="#ff7a7a7a"
                android:textSize="@dimen/des_tv_size" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/des_layout_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/des_iv_4"
                android:layout_width="@dimen/des_iv_width"
                android:layout_height="@dimen/des_iv_height"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/des_tv_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="#ff7a7a7a"
                android:textSize="@dimen/des_tv_size" />
        </LinearLayout>
    </LinearLayout>
  • 初始化布局(用的是xutil框架)

    @ViewInject(R.id.safe_content)
    private LinearLayout safe_content;

  • 最初, 该布局是收缩的

    ViewGroup.LayoutParams layoutParams = safe_content.getLayoutParams();//拿到layoutParam
    layoutParams.height = 0;//设置其height为0
    safe_content.setLayoutParams(layoutParams);//再作为参数设置回去

  • 点击之后,展开动画

    • 定义两个变量:起点和终点。代表动画执行的范围
    • 调用ofInt方法,确定动画执行范围
    • 添加addUpdateListener,监听Animation变化
    • 拿到Animation的值,赋给layoutParam.height,这样当动画执行时,layout的高度不断增加,也就是不断展开的效果
    • 添加addListenter,编写动画结束后的动作
    • 启动动画
boolean flag = false;
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.safe_layout){
            int startHeight,targetHeight;
            if(!flag){/**展开的动画**/
                startHeight = 0;
                targetHeight = getMeasureHeight();
                flag = true;
            }else {
                targetHeight = 0;
                startHeight = getMeasureHeight();
                flag = false;
            }
            /**值动画*/
            ValueAnimator animator = ValueAnimator.ofInt(startHeight,targetHeight);
            final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) safe_content.getLayoutParams();
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                /**监听值的变化*/
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();/**运行当前时间点的一个值*/
                    layoutParams.height = value;
                    safe_content.setLayoutParams(layoutParams);/**刷新界面*/
                }
            });
            animator.addListener(new Animator.AnimatorListener() {
                /**监听动画执行,当动画开始执行的时候调用*/


                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if(flag){
                        safe_arrow.setImageResource(R.drawable.arrow_up);
                    }else {
                        safe_arrow.setImageResource(R.drawable.arrow_down);
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }

            });
            animator.setDuration(500);
            animator.start();
        }

    }

    private int getMeasureHeight() {
        int width = safe_content.getMeasuredWidth();/**由于宽度不会发生变化  宽度的值取出来*/
        safe_content.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        /**参数1  测量控件mode    参数2  大小*/
        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(250, MeasureSpec.AT_MOST);
        /** 测量规则 宽度是一个精确的值width, 高度最大是1000,以实际为准*/
        safe_content.measure(widthMeasureSpec,heightMeasureSpec);
        return safe_content.getMeasuredHeight();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值