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);
}
});