自己实现的一个不会奇奇怪怪换行的TextView.

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by songxun.sx on 2015/7/20.
 */
public class TextDrawView extends View {
    private int textColor = 0xffafafaf;
    private float textSize = 12;
    private String textToDraw = "";
    private List<String> textLines=new ArrayList<String>();
    private float lineMul=1.3f;
    TextPaint textPaint;

    public TextDrawView(Context context) {
        super(context);
        init();
    }

    public TextDrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TextDrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        setTextColor(textColor);
        setTextSize(textSize);
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textPaint.setColor(textColor);
    }

    /**
     * µ¥Î»sp
     */
    public void setTextSize(float textSize) {
        Context context = this.getContext();
        Resources r = Resources.getSystem();
        if (context != null) {
            r = context.getResources();
        }
        this.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, r.getDisplayMetrics());
        textPaint.setTextSize(this.textSize);
    }

    public void setText(String textToDraw) {
        if (textToDraw == null) {
            return;
        }
        this.textToDraw = textToDraw;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        textLines.clear();
        while (textToDraw.length() > 0) {
            int size = textPaint.breakText(textToDraw, true, getMeasuredWidth(),
                    null);
            textLines.add(textToDraw.substring(0, size));
            textToDraw = textToDraw.substring(size);
        }
        int width, height;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = getMeasuredWidth();
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = textLines.size()*(int)(textPaint.getTextSize()*lineMul)+1;
        }
        setMeasuredDimension(width, height);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float x=0;
        float y=textPaint.getTextSize();
        for(String text:textLines){
            canvas.drawText(text,x,y,textPaint);
            y=y+textPaint.getTextSize()*lineMul;
        }

    }
}

github地址:https://github.com/sddyljsx/Android-TextView-not-change-line-in-advance/blob/master/TextDrawView.java


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值