点击箭头隐藏折叠view

本文探讨如何创建一个自定义的ViewGroup,通过点击箭头来控制子视图的折叠与展开。主要涉及对layoutParam的bottomMargin属性的理解和使用,以实现内容的动态隐藏和显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义ViewGroup,对设置layoutParam的bottomMargin不太理解

package com.aiyuba.animateview;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyuba.uiview.R;

/**
 * Created by maoyujiao on 2020/4/11.
 */

public class ExpandView extends FrameLayout {
    private static final String TAG = "ExpandView";

    private final View view;
    private TextView expand;
    private ImageView arrow;
    private boolean isClose = true;
    private RotateAnimation rotateAnimation;


    public ExpandView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.expand_view,this,true);
        expand = view.findViewById(R.id.expand);
        arrow = view.findViewById(R.id.img_arrow);
        arrow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                initAnimation();
            }
        });
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //默认隐藏
        final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) expand.getLayoutParams();
        int expandHeight = expand.getMeasuredHeight();
        marginLayoutParams.bottomMargin = -expandHeight;
        expand.setLayoutParams(marginLayoutParams);
    }

    private void initAnimation() {
        if(isClose) {
            rotateAnimation = new RotateAnimation(0, 180,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        }else {
            rotateAnimation = new RotateAnimation(180, 360,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        }
        rotateAnimation.setDuration(500);
        rotateAnimation.setFillAfter(true);
        arrow.startAnimation(rotateAnimation);

        final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) expand.getLayoutParams();
        int expandHeight = expand.getMeasuredHeight();
        ValueAnimator valueAnimator = isClose ? ValueAnimator.ofInt(-expandHeight,0)
                :ValueAnimator.ofInt(0,-expandHeight);
        valueAnimator.setDuration(500);
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int height = (int)animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: " + height);
                marginLayoutParams.bottomMargin = height;//不太懂bottomMargin的作用
//                marginLayoutParams.height = height; //设置height第一次可以,当height变为0后,getMeasuredHeight就一直为0了
                expand.setLayoutParams(marginLayoutParams);
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                isClose = !isClose;
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

}

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值