Android TextView多行展示文本,多余"..."省略,文末用".pdf"格式结尾

如题需求:UED图如下,


image.png

如图,也就是文末用....pdf结尾。

碰到的主要问题:如何在recycleview中动态计算 textview的宽高。考虑到recycleview 复用机制无法拿到TextView的宽高,使用自定义TextView来获取宽高。
具体看代码:

public class MyEllipsisTextView extends AppCompatTextView {

    private static final String TAG = MyEllipsisTextView.class.getSimpleName();
    private String mText;
    private int mTextColor;
    private float mTextSize;


    private Paint mPaint;
    private Rect mBound;
    private int mWidth;


    public MyEllipsisTextView(Context context) {
        this(context, null);
    }

    public MyEllipsisTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyEllipsisTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyEllipsisTextView, defStyleAttr, 0);
        mText = a.getString(R.styleable.MyEllipsisTextView_mText);
        mTextSize = a.getDimension(R.styleable.MyEllipsisTextView_mTextSize, 15f);
        mTextColor = a.getColor(R.styleable.MyEllipsisTextView_mTextColor, Color.YELLOW);
        a.recycle();
        init();
    }

    private void init() {

        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        mBound = new Rect();

        mPaint.getTextBounds(mText, 0, mText.length(), mBound);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //1. 获取 自定义 View 的宽度,高度 的模式
        int heigthMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        mWidth = width;

        if (MeasureSpec.AT_MOST == heigthMode) {
            Rect bounds = new Rect();
            mPaint.getTextBounds(mText, 0, mText.length(), bounds);
            height = bounds.height() + getPaddingBottom() + getPaddingTop();
        }

        if (MeasureSpec.AT_MOST == widthMode) {
            Rect bounds = new Rect();
            mPaint.getTextBounds(mText, 0, mText.length(), bounds);
            width = bounds.width() + getPaddingLeft() + getPaddingRight();
        }
        setMeasuredDimension(width, height);
        setMyText();
    }

    private void setMyText() {
        setTextColor(mTextColor);
        setTextSize(mTextSize);
        setWidth(mWidth);
        setHeight(Utils.getFontHeight(mTextSize) * 2);
        setText(Utils.ellipsizeString(this, mText, mWidth));
    }

    public void setInitText(String charSequence) {
        mText = charSequence;
    }


//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        canvas.drawText(mText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
//        Log.d(TAG, "setText:  canvas:" + canvas.toString());
//    }


//    @Override
//    public void setText(CharSequence text, BufferType type) {
//        super.setText(text, type);
//        Log.d(TAG, "setText:  text:" + text + ",type:" + type);
////        setText(mText);
////        setTextColor(mTextColor);
////        setTextSize(mTextSize);
//        setWidth(mWidth);
//    }
}
 
        
        
        
    
    /**
     * 字符串显示到textView, textView maxLines=2
     * 如果字符串太长显示不下,则用省略号代替
     * 省略号的位置在第一行末尾
     *
     * @param textView 显示字符串的view
     * @param str      要显示的字符串
     * @param width    显示字符串的view的宽
     * @return 处理后带省略号的字符串
     */
    public static String ellipsizeString(TextView textView, String str, int width) {
        if (TextUtils.isEmpty(str)) return str;
        Paint paint = textView.getPaint();
        //文字总宽小于2倍的view宽,说明小于2行,直接返回
        if (paint.measureText(str) < 2 * width) {
            return str;
        }

        //存储显示到view的每行文字
        List list = new ArrayList<>();

        int len = 0;
        int start, end = 0;

        while (len < str.length()) {
            len += end;
            int count = paint.breakText(str, end, str.length(), true, width, null);
            start = end;
            end = end + count;
            list.add(str.substring(start, end));
        }

        String line1 = list.get(0);
        String line2 = list.get(1);
        int line2Width = (int) paint.measureText(line2);
        String[] split1 = str.split("\\.");
        if (split1.length < 2) return str;
        String mString = "...." + split1[1];
        int subWidth = (int) paint.measureText(mString);
        int widthTemp = line2Width;
        while (widthTemp + subWidth > line2Width) {
            if (line2.length()<1) break;
            line2 = line2.substring(0, line2.length() - 1);
            widthTemp = (int) paint.measureText(line2);
        }
        return line1 + line2 + mString;
    }

    public static int getFontHeight(float fontSize)
    {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.top) + 2;
    }

使用

txt = "看了就变聪明怎么都漂亮的不灵不灵的文档收到货水电费可接受的山东矿机看了就变聪明怎么都漂亮的不灵不灵的文档收到货水电费可接受的山东矿机.pdf";

myText.setInitText(txt);
S90715-152327.jpg

你可能感兴趣的:(Android TextView多行展示文本,多余"..."省略,文末用".pdf"格式结尾)