drawText注意事项

Posted iblade

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了drawText注意事项相关的知识,希望对你有一定的参考价值。

自定义View中写文字坑多多 总结一下:

drawText()参数中y不是文字左上角坐标,是基线的y坐标;

drawText用于单行文字绘制,多行文字可以使用StaticLayout;

 

 

 

 

public class TextItemView extends View {
    private Paint pointPaint;
    private TextPaint textPaint;
    private Context context;
    private float fontSpacing, halfSpacing;
    private List<String> mListTexts = new ArrayList<>();
    private List<StaticLayout> mListStaticLayouts = new ArrayList<>();

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

    public TextItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        pointPaint.setColor(Color.parseColor("#666666"));

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.parseColor("#666666"));
        textPaint.setTextSize(BaseUtils.dip2px(context, 16));
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        fontSpacing = fontMetrics.bottom - fontMetrics.top;
        halfSpacing = fontSpacing / 2.0f;
    }


    public void setData(List<String> list) {
        mListTexts.clear();
        mListTexts.addAll(list);
        mListStaticLayouts.clear();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mListTexts.isEmpty()) {
            return;
        }
        int y = 0;//开始 draw的Y左边
        if (mListStaticLayouts.isEmpty()) {
            int txtWidth = (int) (width - fontSpacing);//总宽 - 圆点宽 = 文字宽
            for (String s : mListTexts) {
                StaticLayout layout = new StaticLayout(s, textPaint, txtWidth, Layout.Alignment.ALIGN_NORMAL, 1.3f, 0, true);
                mListStaticLayouts.add(layout);
                LogUtils.d("@@@", ", width = " + width + " , s = " + s);
            }
        }

        for (StaticLayout mStaticLayout : mListStaticLayouts) {
            canvas.drawCircle(halfSpacing, y + halfSpacing, 4, pointPaint);
            canvas.save();
            canvas.translate(fontSpacing, y);//偏移 腾出圆点的位置
            mStaticLayout.draw(canvas);
            int multiplier = (int) (fontSpacing * (mStaticLayout.getSpacingMultiplier() - 1.0f));
            y += (mStaticLayout.getHeight() + multiplier);
            canvas.restore();
            LogUtils.d("@@@", "y  = " + y);
        }

        if (reHeight != y) {//y是最后一行文字Baseline坐标,才是该控件真正高度,需要重新赋值给该View
            requestLayout();
            reHeight = y;
        }
    }

    private int reHeight;
    private int width;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        LogUtils.d("@@@", "onSizeChanged , width = " + width);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int finalHeight = resolveSize(Math.max(reHeight, 1), heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, finalHeight);
    }
}

 

以上是关于drawText注意事项的主要内容,如果未能解决你的问题,请参考以下文章

如何使 drawText() 准确定位?

Android Canvas drawText实现中文垂直居中

在位图上使用 DrawText() 时文本旋转?

如何使用Canvas绘制drawText旋转角度

MFC 画字体DrawText()或TextOut(),CFont字体样式类

小程序各种功能代码片段整理---持续更新