textview折叠效果

ViewTreeObserver vto = tv1.getViewTreeObserver();
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        int lineCount = tv1.getLineCount();
                        //LogUtils.i("当前文本lineCount:" + lineCount+";isExpandText:"+dataList.isExpandText);
                        if (lineCount > 3) {
                           // holder.item_latest_expand_imageview.setImageDrawable(UIUtils.tintDrawable(UIUtils.getDrawable(R.mipmap.expand_textview), R.attr.colorTheme));
                            holder.item_latest_expand_ll.setVisibility(View.VISIBLE);
                            if (dataList.isExpandText) {//展开状态
                                //翻转icon的180度旋转动画
                                holder.item_latest_expand_textview.setText("收起全部");
                                holder.item_latest_expand_imageview.setImageDrawable(UIUtils.tintDrawable(UIUtils.getDrawable(R.mipmap.no_expand_textview), R.attr.colorTheme));
                                int height = tv1.getLineHeight() * tv1.getLineCount();
                              //  if (tv1.getHeight() != height) {
                                    tv1.setHeight(height);
                                //}
                            } else {//折叠
                                holder.item_latest_expand_imageview.setImageDrawable(UIUtils.tintDrawable(UIUtils.getDrawable(R.mipmap.expand_textview), R.attr.colorTheme));
                                holder.item_latest_expand_textview.setText("展开更多");
                                int height = tv1.getLineHeight() * 3;
                               // if (tv1.getHeight() != height) {
                                    tv1.setHeight(height);
                              //  }
                            }
                        } else {
                            holder.item_latest_expand_ll.setVisibility(View.GONE);
                            int height = tv1.getLineHeight() * tv1.getLineCount();
                            if (tv1.getHeight() != height) {
                                tv1.setHeight(height);
                            }
                        }
                        return true;
                    }
                });

                holder.item_latest_expand_ll.setOnClickListener(new View.OnClickListener() {//点击展开/折叠
                    @Override
                    public void onClick(View v) {
                        dataList.isExpandText = !dataList.isExpandText;
                        tv1.clearAnimation();  //清除动画
                        holder.item_latest_expand_imageview.clearAnimation();
                        final int tempHight;
                        final int startHight = tv1.getHeight();  //起始高度
                        int durationMillis = 500;

                        if (!dataList.isExpandText) {
                            /**
                             * 折叠效果,从长文折叠成短文
                             */
                            tempHight = tv1.getLineHeight() * 3 - startHight;
                            //翻转icon的180度旋转动画
                            RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                            animation.setDuration(durationMillis);
                            animation.setFillAfter(false);
                            holder.item_latest_expand_imageview.startAnimation(animation);
                            holder.item_latest_expand_textview.setText("展开更多");
                        } else {
                            /**
                             * 展开效果,从短文展开成长文
                             */
                            tempHight = tv1.getLineHeight() * tv1.getLineCount() - startHight;
                            //翻转icon的180度旋转动画
                            RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                            animation.setDuration(durationMillis);
                            animation.setFillAfter(false);
                            holder.item_latest_expand_imageview.startAnimation(animation);
                            holder.item_latest_expand_textview.setText("收起全部");
                        }
                        Animation animation = new Animation() {
                            //interpolatedTime 为当前动画帧对应的相对时间,值总在0-1之间
                            protected void applyTransformation(float interpolatedTime, Transformation t) { //根据ImageView旋转动画的百分比来显示textview高度,达到动画效果
                                tv1.setHeight((int) (startHight + tempHight * interpolatedTime));//原始长度+高度差*(从0到1的渐变)即表现为动画效果

                            }
                        };
                        animation.setDuration(durationMillis);
                        tv1.startAnimation(animation);
                    }
                });

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值