Android技术知识Android TipsAndroid开发经验谈

TextView实现展开收起的效果

2015-11-08  本文已影响11201人  alighters

在做Android的过程中,我们经常会遇到TextView显示文本过长的情况,这里我们以开源库ExpandableTextView为例,对其的实现做一讲解:

实现原理描述:expandableTextView继承自LinearLayout(只支持竖直方向),包含TextView和ImageButton两个子view,通过参数 maxCollapsedLines来设置折叠最大的行数,在onMeasure方法中,判断textView的行数,来进行textview折叠和收起状态的设定;另外,收起和展开的效果则针对TextView的高度进行动画。

** 具体使用方法** :

** 实现细节:**

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// If no change, measure and return

if (!mRelayout || getVisibility() == View.GONE) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

return;

}

mRelayout = false;

// Setup with optimistic case

// i.e. Everything fits. No button needed

mButton.setVisibility(View.GONE);

mTv.setMaxLines(Integer.MAX_VALUE);

// Measure

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

// If the text fits in collapsed mode, we are done.

if (mTv.getLineCount() <= mMaxCollapsedLines) {

return;

}

// Saves the text height w/ max lines

mTextHeightWithMaxLines = getRealTextViewHeight(mTv);

// Doesn't fit in collapsed mode. Collapse text view as needed. Show

// button.

if (mCollapsed) {

mTv.setMaxLines(mMaxCollapsedLines);

}

mButton.setVisibility(View.VISIBLE);

// Re-measure with new setup

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (mCollapsed) {

// Gets the margin between the TextView's bottom and the ViewGroup's bottom

mTv.post(new Runnable() {

@Override

public void run() {

mMarginBetweenTxtAndBottom = getHeight() - mTv.getHeight();

}

});

// Saves the collapsed height of this ViewGroup

mCollapsedHeight = getMeasuredHeight();

}

}
class ExpandCollapseAnimation extends Animation {

private final View mTargetView;

private final int mStartHeight;

private final int mEndHeight;

public ExpandCollapseAnimation(View view, int startHeight, int endHeight) {

mTargetView = view;

mStartHeight = startHeight;

mEndHeight = endHeight;

setDuration(mAnimationDuration);

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

final int newHeight = (int)((mEndHeight - mStartHeight) * interpolatedTime + mStartHeight);

mTv.setMaxHeight(newHeight - mMarginBetweenTxtAndBottom);

if (Float.compare(mAnimAlphaStart, 1.0f) != 0) {

applyAlphaAnimation(mTv, mAnimAlphaStart + interpolatedTime * (1.0f - mAnimAlphaStart));

}

mTargetView.getLayoutParams().height = newHeight;

mTargetView.requestLayout();

}

@Override

public void initialize( int width, int height, int parentWidth, int parentHeight ) {

super.initialize(width, height, parentWidth, parentHeight);

}

@Override

public boolean willChangeBounds( ) {

return true;

}

};

动画调用的代码是在view的点击事件回调中:

Animation animation;

if (mCollapsed) {

animation = new ExpandCollapseAnimation(this, getHeight(), mCollapsedHeight);

} else {

animation = new ExpandCollapseAnimation(this, getHeight(), getHeight() +

mTextHeightWithMaxLines - mTv.getHeight());

}

我们这里就会产生一个疑惑,mTextHeightWithMaxLines(文本全部显示对应的textview的高度)是怎么获取到的呢?相应获取的代码如下:

private static int getRealTextViewHeight(@NonNull TextView textView) {

int textHeight = textView.getLayout().getLineTop(textView.getLineCount());

int padding = textView.getCompoundPaddingTop() + textView.getCompoundPaddingBottom();

return textHeight + padding;

}

这里有两个主要的方法,getLineCount获取textview的行数,getLineTop获取textview的行高。
至此,我们就可以实现整个expandableTextView的收起展开的效果了。

public void setText(@Nullable CharSequence text, @NonNull SparseBooleanArray collapsedStatus, int position) {

mCollapsedStatus = collapsedStatus;

mPosition = position;

boolean isCollapsed = collapsedStatus.get(position, true);

clearAnimation();

mCollapsed = isCollapsed;

mButton.setImageDrawable(mCollapsed ? mExpandDrawable : mCollapseDrawable);

setText(text);

getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;

requestLayout();

}

最后,我们关于ExpandableTextView的实现原理就讲完了,具体还有疑问的话,可以提出一起讨论。另外,代码的格式没有做缩进,大家多多担待。灰常感谢。。

上一篇下一篇

猜你喜欢

热点阅读