使用TextPaint的子类时,StaticLayout连字符断开

时间:2017-11-30 18:35:16

标签: java android

我发现在我使用StaticLayout的自定义视图中,如果我使用TextPaint的子类而不是TextPaint本身,则连字符会中断。

我的自定义视图:

public class MyView extends View {

    String mText = "Using TextPaint: Recommendations";
    TextPaint mTextPaint;
    StaticLayout mStaticLayout;

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

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

    private void init() {
        mTextPaint = new TextPaint2();    // Works fine with TextPaint();
        mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);

        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        int width = Math.round(100 * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));

        StaticLayout.Builder builder = StaticLayout.Builder.obtain(mText, 0, mText.length(), mTextPaint, width)
                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_FULL);
        mStaticLayout = builder.build();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        mStaticLayout.draw(canvas);
        canvas.restore();
    }
}

我的TextPaint的子类:

public class TextPaint2 extends TextPaint {
    // Doesn't override anything
}

有谁知道这里发生了什么?这是一个错误,如果是这样,是否有解决方法?

截图: Screenshot Example

0 个答案:

没有答案