TextView文字的折叠与展开

xml部分:

<ImageView
	android:id="@+id/ivCollaps"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_centerVertical="true"
	android:layout_alignParentRight="true"
	android:layout_marginRight="10dp"
	android:src="@drawable/ico_zhedie"/>


<TextView
	android:id="@+id/tvSelfIntrodution"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:paddingLeft="10dp"
	android:paddingRight="10dp"
	android:maxLines="3"/>

代码部分:

tvSelfIntrodution.post(new Runnable() {
	@Override public void run() {
		ivCollaps.setVisibility(tvSelfIntrodution.getLineCount() > 3 ? View.VISIBLE : View.GONE);
	}
});
ivCollaps.setOnClickListener(new View.OnClickListener() {
	boolean isExpand;//是否已展开的状态
	@Override public void onClick(View view) {
		isExpand = !isExpand;
		tvSelfIntrodution.clearAnimation();//清除动画效果
		final int deltaValue;//默认高度,即前边由maxLine确定的高度
		final int startValue = tvSelfIntrodution.getHeight();//起始高度
		int durationMillis = 350;//动画持续时间
		if (isExpand) {
			/**
			 * 折叠动画
			 * 从实际高度缩回起始高度
			 */
			deltaValue = tvSelfIntrodution.getLineHeight() * tvSelfIntrodution.getLineCount() - startValue;
			RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			animation.setDuration(durationMillis);
			animation.setFillAfter(true);
			ivCollaps.startAnimation(animation);
		} else {
			/**
			 * 展开动画
			 * 从起始高度增长至实际高度
			 */
			deltaValue = tvSelfIntrodution.getLineHeight() * 3 - startValue;
			RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			animation.setDuration(durationMillis);
			animation.setFillAfter(true);
			ivCollaps.startAnimation(animation);
		}
		Animation animation = new Animation() {
			protected void applyTransformation(float interpolatedTime, Transformation t) { //根据ImageView旋转动画的百分比来显示textview高度,达到动画效果
				tvSelfIntrodution.setHeight((int) (startValue + deltaValue * interpolatedTime));
			}
		};
		animation.setDuration(durationMillis);
		tvSelfIntrodution.startAnimation(animation);
	}
});

转载于:https://my.oschina.net/shenhuniurou/blog/950343

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值