33import android .annotation .SuppressLint ;
44import android .content .Context ;
55import android .graphics .Color ;
6+ import android .graphics .PorterDuff ;
67import android .graphics .drawable .Drawable ;
78import android .text .Layout ;
89import android .text .SpannableString ;
910import android .text .Spanned ;
1011import android .text .StaticLayout ;
1112import android .text .TextPaint ;
1213import android .text .style .DynamicDrawableSpan ;
13- import android .text .style .ForegroundColorSpan ;
1414import android .text .style .ImageSpan ;
15+ import android .util .Log ;
1516import android .view .View ;
1617import android .widget .TextView ;
17- import android .widget .Toast ;
1818
1919import androidx .core .content .ContextCompat ;
2020
2525 * @since 04-07-2020
2626 */
2727public class MyMoreText implements View .OnClickListener {
28- private SpannableString eclipseString ; //收起的文字
29- private SpannableString notEclipseString ; //展开的文字
28+ private static final String TAG = "MyMoreText" ;
29+ private SpannableString foldString ; //收起的文字
30+ private SpannableString expandString ; //展开的文字
3031 private TextView textView ;
3132
3233 private static class MoreTextHolder {
@@ -46,42 +47,49 @@ public void getLastIndexForLimit(TextView tv, int maxLine, String content) {
4647 //每行文本的布局宽度
4748 int width = context .getResources ().getDisplayMetrics ().widthPixels - dip2px (context , 40 );
4849 //实例化StaticLayout 传入相应参数
49- StaticLayout staticLayout = new StaticLayout (content , paint , width , Layout .Alignment .ALIGN_NORMAL , 1 , 0 , false );
50+ StaticLayout staticLayout = new StaticLayout (content , paint , width , Layout .Alignment .ALIGN_NORMAL ,
51+ 1 , 0 , false );
5052 //判断content是行数是否超过最大限制行数3行
5153 if (staticLayout .getLineCount () > maxLine ) {
5254 //定义展开后的文本内容
5355 String string1 = content + " 收起" ;
54- notEclipseString = new SpannableString (string1 );
56+ expandString = new SpannableString (string1 );
5557 //给收起两个字设成蓝色
56- notEclipseString .setSpan (new ForegroundColorSpan (Color .parseColor ("#ffffff" )), string1 .length () - 2 , string1 .length (), Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
58+ // notEclipseString.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffff")),
59+ // string1.length() - 2, string1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
5760
5861 Drawable drawable = ContextCompat .getDrawable (context , R .drawable .ic_expand_less_black_24dp );
5962 if (drawable != null ) {
63+ Log .e (TAG , "getLastIndexForLimit: w " + drawable .getIntrinsicWidth ());
64+ Log .e (TAG , "getLastIndexForLimit: h " + drawable .getIntrinsicHeight ());
65+ Log .e (TAG , "24dp : " + ActivityExtKt .getDp (24 ));
6066 string1 = string1 + "v" ;
61- notEclipseString = new SpannableString (string1 );
62- drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight ());
63- ImageSpan imageSpan = new ImageSpan (drawable , DynamicDrawableSpan .ALIGN_BOTTOM );
64- notEclipseString .setSpan (imageSpan , string1 .length () - 1 , string1 .length (), Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
67+ expandString = new SpannableString (string1 );
68+ drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth () / 2 , drawable .getIntrinsicHeight () / 2 );
69+ ImageSpan imageSpan = new ImageSpan (drawable , DynamicDrawableSpan .ALIGN_BASELINE );
70+ drawable .setTintMode (PorterDuff .Mode .SRC_OUT );
71+ drawable .setTint (Color .RED );
72+ expandString .setSpan (imageSpan , string1 .length () - 1 , string1 .length (), Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
6573 }
6674
6775 //获取到第三行最后一个文字的下标
6876 int index = staticLayout .getLineStart (maxLine ) - 1 ;
6977 //定义收起后的文本内容
7078 String substring = content .substring (0 , index - 1 ) + "..." + "∨" ;
71- eclipseString = new SpannableString (substring );
79+ foldString = new SpannableString (substring );
7280 drawable = ContextCompat .getDrawable (context , R .drawable .ic_expand_more_black_24dp );
7381 if (drawable != null ) {
74-
75- drawable .setBounds (0 , 0 , 50 , 50 );
82+ drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth () / 2 , drawable .getIntrinsicHeight () / 2 );
83+ drawable .setTintMode (PorterDuff .Mode .SRC_OUT );
84+ drawable .setTint (Color .RED );
7685 ImageSpan imageSpan = new ImageSpan (drawable , DynamicDrawableSpan .ALIGN_CENTER );
77- Toast .makeText (context , "length " + string1 .length (), Toast .LENGTH_SHORT ).show ();
78- eclipseString .setSpan (imageSpan , substring .length () - 1 , substring .length (), Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
86+ foldString .setSpan (imageSpan , substring .length () - 1 , substring .length (), Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
7987 }
8088
8189 //给查看全部设成蓝色
8290// eclipseString.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffff")), substring.length() - 1, substring.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8391 //设置收起后的文本内容
84- tv .setText (eclipseString );
92+ tv .setText (foldString );
8593 tv .setOnClickListener (this );
8694 //将textView设成选中状态 true用来表示文本未展示完全的状态,false表示完全展示状态,用于点击时的判断
8795 tv .setSelected (true );
@@ -104,11 +112,11 @@ private static int dip2px(Context mContext, float dpValue) {
104112 public void onClick (View view ) {
105113 if (view .isSelected ()) {
106114 //如果是收起的状态
107- textView .setText (notEclipseString );
115+ textView .setText (expandString );
108116 textView .setSelected (false );
109117 } else {
110118 //如果是展开的状态
111- textView .setText (eclipseString );
119+ textView .setText (foldString );
112120 textView .setSelected (true );
113121 }
114122 }
0 commit comments