1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| public class StretchTextView extends android.support.v7.widget.AppCompatTextView { public static final String TAG = "StretchTextView";
private boolean hasMulti; private OnClickListener fullTextClickListener; private String fullText;
public StretchTextView(Context context) { this(context, null); }
public StretchTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public StretchTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); }
private void init() { setTextColor(getResources().getColor(R.color.darkGray)); setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); setLineSpacing(DensityUtils.dp2px(getContext(), 4), 1.0f); setMovementMethod(LinkMovementMethod.getInstance()); }
public void setString(String text, OnClickListener fullTextClickListener) { if (TextUtils.isEmpty(text)) { setText(""); setVisibility(GONE); return; } setVisibility(VISIBLE); this.fullTextClickListener = fullTextClickListener; this.fullText = text; double count = 0; int lineCount = 0; StringBuffer sb = new StringBuffer(); for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (isChinese(ch)) count += 1.0f; else count += 0.5f;
sb.append(ch);
if (count >= 144) break;
if (isNewLinsChar(ch)) lineCount++; if (lineCount > 4) break;
} if (isNewLinsChar(sb.charAt(sb.length() - 1))) sb = sb.deleteCharAt(sb.length() - 1);
hasMulti = count > 144; if (!hasMulti) hasMulti = lineCount > 4;
if (hasMulti) sb.append("... 全文"); mySetText(sb.toString()); }
private void mySetText(String text) { setText(getClickableSpan(text)); }
private SpannableString getClickableSpan(String text) {
SpannableString spannableInfo = new SpannableString(text);
if (hasMulti) { spannableInfo.setSpan(new Clickable(fullTextClickListener, R.color.blue), text.length() - 2, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); } List<UrlTarget> urlTargets = myGetUrls();
if (urlTargets.isEmpty()) spannableInfo.setSpan(new Clickable(fullTextClickListener, R.color.darkGray), 0, hasMulti ? text.length() - 6 : text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); else { int length = hasMulti ? text.length() - 6 : text.length(); int lastNormalStart = 0; int lastEnd = 0; for (int i = 0; i <= length; i++) { boolean isurl = false; for (UrlTarget target : urlTargets) { if (target.has(i)) {
if (i > 0) spannableInfo.setSpan( new Clickable(fullTextClickListener, R.color.darkGray), lastNormalStart, i, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (!target.isHandle) { spannableInfo.setSpan( new Clickable(v -> { WebActivity.startActivity(getContext(), target.url); }, R.color.blue), target.start, (target.end >= length ? length : target.end), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); target.isHandle = true; lastNormalStart = target.end; lastEnd = target.end; i = lastNormalStart; } break; } } } if (lastEnd < length) { spannableInfo.setSpan(new Clickable(fullTextClickListener, R.color.darkGray), lastEnd, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); }
}
return spannableInfo; }
private class UrlTarget { String url; int start; int end; boolean isHandle;
private boolean has(int location) { return location >= start && location <= end; }
@Override public String toString() { return "UrlTarget{" + "url='" + url + '\'' + ", start=" + start + ", end=" + end + '}'; } }
private class Clickable extends ClickableSpan implements View.OnClickListener { private final int textColor; private View.OnClickListener mListener;
public Clickable(View.OnClickListener l, int color) { mListener = l; textColor = color; }
@Override public void onClick(View v) { mListener.onClick(v); ((TextView) v).setHighlightColor(Color.argb(0, 0, 0, 0)); }
@Override public void updateDrawState(TextPaint ds) { ds.setColor(getResources().getColor(textColor)); ds.setUnderlineText(false); } }
private List<UrlTarget> myGetUrls() { List<UrlTarget> urlTargets = new ArrayList<>(); String fullText = this.fullText; for (; ; ) { String url = getCompleteUrl(fullText); if (TextUtils.isEmpty(url)) { break; } int start = fullText.indexOf(url); fullText = fullText.substring(start + url.length());
UrlTarget urlTarget = new UrlTarget(); if (!urlTargets.isEmpty()) { UrlTarget lastUrlTarget = urlTargets.get(urlTargets.size() - 1); start = lastUrlTarget.end + start; }
urlTarget.url = url; urlTarget.start = start; urlTarget.end = start + url.length(); urlTargets.add(urlTarget); }
Log.e(TAG, urlTargets.toString()); return urlTargets; }
private static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; }
private static boolean isNewLinsChar(char c) { return c == '\r' || c == '\n'; }
private static String getCompleteUrl(String text) { Pattern p = Pattern.compile("((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?", Pattern.CASE_INSENSITIVE); Matcher matcher = p.matcher(text); if (matcher.find()) { return matcher.group(); } return ""; } }
|