From a5fb5bc399b18bb9db5422c5d86d4aef533005c5 Mon Sep 17 00:00:00 2001 From: Bob Peng <445947962@qq.com> Date: Tue, 2 Mar 2021 12:30:50 +0800 Subject: [PATCH] Revert "don't append collapse symbol when collapse span is null" --- .../com/lsjwzh/widget/text/FastTextView.java | 54 ++++--------------- .../lsjwzh/widget/text/ReadMoreTextView.java | 7 +-- 2 files changed, 11 insertions(+), 50 deletions(-) diff --git a/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java b/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java index 024b974..37e28a8 100644 --- a/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java +++ b/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/FastTextView.java @@ -6,7 +6,6 @@ import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; import android.os.Build; import android.support.annotation.NonNull; @@ -17,7 +16,6 @@ import android.text.LayoutUtils; import android.text.Spannable; import android.text.SpannableString; -import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.StaticLayout; import android.text.StaticLayoutBuilderCompat; @@ -25,13 +23,11 @@ import android.text.TextPaint; import android.text.TextUtils; import android.text.style.ReplacementSpan; -import android.text.util.Linkify; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.MotionEvent; -import java.util.regex.Pattern; /** * Simple and Fast TextView. @@ -45,9 +41,6 @@ public class FastTextView extends FastTextLayoutView { private boolean mEnableLayoutCache = false; // experiment private EllipsisSpannedContainer mEllipsisSpanned; private int mCurTextColor; - protected boolean mCompressText; - protected int mLinkifyMask; - private int mLinkColor = Color.parseColor("#109DD0"); public FastTextView(Context context) { this(context, null); @@ -173,6 +166,7 @@ public boolean onTouchEvent(MotionEvent event) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + long start = System.currentTimeMillis(); int width = MeasureSpec.getSize(widthMeasureSpec); boolean exactly = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY; if (!exactly) { @@ -204,6 +198,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } + + long end = System.currentTimeMillis(); + if (BuildConfig.DEBUG) { + Log.d(TAG, "onMeasure cost:" + (end - start)); + } } protected boolean shouldResetStaticLayout(int width, CharSequence text, Layout layout) { @@ -346,7 +345,7 @@ public void setMaxLines(int maxLines) { * TypedValue} for the possible dimension units. * * @param textSize The desired size in the given units. - * @param unit The desired dimension unit. + * @param unit The desired dimension unit. */ public void setTextSize(float textSize, int unit) { float rawTextSize = TypedValue.applyDimension( @@ -384,42 +383,8 @@ public void setCustomEllipsisSpan(ReplacementSpan customEllipsisSpan) { mCustomEllipsisSpan = customEllipsisSpan; } - public void compressText(boolean enable) { - mCompressText = enable; - } - - public void addLinks(int mask) { - mLinkifyMask = mask; - mTextPaint.linkColor = mLinkColor; - } - @NonNull protected StaticLayout makeLayout(CharSequence text, int maxWidth, boolean exactly) { - if (mCompressText) { - SpannableStringBuilder ssb = new SpannableStringBuilder(); - String[] patterns = Pattern.compile("\n").split(text); - int patternSize = patterns.length; - int realCount = 0; - for (int i = 0; i < patternSize; i++) { - realCount++; - if (patterns[i].isEmpty()) continue; - ssb.append(patterns[i]); - if (i < patternSize - 1) { - ssb.append("\n"); - } - } - if (realCount >= mAttrsHelper.mMaxLines) { - ssb.append("\n").append("\n"); // extra line to fix ellipse symbol display - } - text = ssb; - } else { - text = new SpannableString(text); // also make text into SpannableString. - } - - if (mLinkifyMask > 0) { - Linkify.addLinks((Spannable) text, mLinkifyMask); - } - TextUtils.TruncateAt truncateAt = getTruncateAt(); int layoutTargetWidth = maxWidth; int contentWidth = maxWidth; @@ -430,6 +395,7 @@ protected StaticLayout makeLayout(CharSequence text, int maxWidth, boolean exact layoutTargetWidth = maxWidth > 0 ? Math.min(maxWidth, contentWidth) : contentWidth; } + StaticLayoutBuilderCompat layoutBuilder = createStaticLayoutBuilder(text, 0, text .length(), mTextPaint, layoutTargetWidth); layoutBuilder.setLineSpacing(mAttrsHelper.mSpacingAdd, mAttrsHelper.mSpacingMultiplier) @@ -474,8 +440,8 @@ protected StaticLayout makeLayout(CharSequence text, int maxWidth, boolean exact } protected StaticLayoutBuilderCompat createStaticLayoutBuilder(CharSequence source, - int start, int end, - TextPaint paint, int width) { + int start, int end, + TextPaint paint, int width) { return StaticLayoutBuilderCompat.obtain(source, start, end, paint, width); } diff --git a/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/ReadMoreTextView.java b/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/ReadMoreTextView.java index 9f2b6c8..ec41919 100644 --- a/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/ReadMoreTextView.java +++ b/widget.FastTextView/src/main/java/com/lsjwzh/widget/text/ReadMoreTextView.java @@ -8,7 +8,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; -import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.StaticLayout; @@ -16,7 +15,6 @@ import android.text.TextPaint; import android.text.TextUtils; import android.text.style.ReplacementSpan; -import android.text.util.Linkify; import android.util.AttributeSet; import android.util.Log; import android.view.View; @@ -119,11 +117,8 @@ protected void onDraw(Canvas canvas) { protected StaticLayout makeLayout(CharSequence text, int maxWidth, boolean exactly) { mWithEllipsisLayout = super.makeLayout(text, maxWidth, exactly); SpannableStringBuilder textWithExtraEnd = new SpannableStringBuilder(text); - if (mLinkifyMask > 0) { - Linkify.addLinks(textWithExtraEnd, mLinkifyMask); - } + textWithExtraEnd.append(COLLAPSE_NORMAL); if (mCollapseSpan != null) { - textWithExtraEnd.append(COLLAPSE_NORMAL); textWithExtraEnd.setSpan(mCollapseSpan, textWithExtraEnd.length() - 1, textWithExtraEnd.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); }