UI控件伸缩动画实现

XML
<LinearLayout
    android:id="@+id/cotainer_more"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/color_white"
    android:orientation="vertical">
    <!--Lazada-->
    <RelativeLayout
        android:id="@+id/container_lazada"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <FrameLayout
            android:id="@+id/img_lazada"
            android:layout_width="45dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="40dp">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/lazada_icon" />
        </FrameLayout>
        <TextView
            android:id="@+id/tv_lazada"
            style="@style/Style_Text_Size_14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/img_lazada"
            android:text="Lazada"
            android:textColor="@color/color_list_text"
            android:textStyle="bold" />
        <ImageView
            android:id="@+id/img_more_anchor_lazada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="40dp"
            android:src="@mipmap/more_1" />
        <TextView
            android:id="@+id/tv_hint_authenticate_lazada"
            style="@style/Style_Text_Size_14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/img_more_anchor_lazada"
            android:text="@string/tidak_sempurna"
            android:textColor="@color/color_hint_text" />
        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_alignParentBottom="true"
            android:background="@color/color_parting_line" />
    </RelativeLayout>
</LinearLayout>
Java代码
@BindView(R.id.cotainer_more)
LinearLayout cotainerMore;

private boolean isClickMore;

//触发动画
containerMore.setOnClickListener(v -> {
    if (!isClickMore) {
        setExpandAnimation(0, SizeUtils.dp2px(50), cotainerMore);
        isClickMore = true;
    } else {
        setExpandAnimation(SizeUtils.dp2px(50), 0, cotainerMore);
        isClickMore = false;
    }
});

/**
 * 动画具体实现
 * @param startSize 开始大小
 * @param endSize 结束大小
 * @param view UI控件
 */
private void setExpandAnimation(int startSize, int endSize, final View view) {
    ValueAnimator animator = ValueAnimator.ofInt(startSize, endSize);
    //动画时长
    animator.setDuration(200);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            Integer value = (Integer) animation.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = value;
            view.setLayoutParams(layoutParams);
        }
    });
    animator.start();
}
效果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值