自动调整TextView的大小以在画布上绘制

时间:2018-07-03 11:43:41

标签: android android-canvas textview

我尝试在PDF document上显示一些文本。该文本可能长也可能短,但应位于一行上。这就是为什么我选择TextViewCompat.setAutoSizeTextTypeWithDefaults实用程序方法,但是在绘制canvas时似乎不起作用的原因。文本被裁剪而不是缩小。

这是我的代码:

PageInfo startingPageInfo = new Builder(STANDARD_PDF_PAGE_WIDTH, STANDARD_PDF_PAGE_HEIGHT, 1).create();
final Page startingPage = document.startPage(startingPageInfo);

Canvas canvas = startingPage.getCanvas();
canvas.save();

final TextView documentTitle = new TextView(this);
TextViewCompat.setAutoSizeTextTypeWithDefaults(documentTitle, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
documentTitle.setLines(1);
documentTitle.setText(String.format("%s - %s", category.getName(), period));
documentTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
documentTitle.setTypeface(null, BOLD);
documentTitle.setTextColor(Color.BLACK);

int measuredWidth = MeasureSpec.makeMeasureSpec(canvas.getWidth() - 100, MeasureSpec.EXACTLY);
int measuredHeight = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);
documentTitle.measure(measuredWidth, measuredHeight);
documentTitle.layout(0, 0, measuredWidth, measuredHeight); 

canvas.translate(titleDx, titleDy);
documentTitle.draw(canvas);

我对Android内部的自定义视图不满意,因此我在这里可能做错了任何事情,我们将不胜感激。

1 个答案:

答案 0 :(得分:3)

重写2 +

您遇到的麻烦是由以下行引起的:

documentTitle.layout(0, 0, measuredWidth, measuredHeight);

此行应为:

documentTitle.layout(0, 0, width, height);

您的代码将宽度和高度设置为一些天文数值,因此文本始终“合适”。

这是(主要是)代码工作的简短演示。我使用了自定义LinearLayout,但这无关紧要。我认为只是上面的更改应该单独起作用,但是我可能还做了一些其他的调整,您可以在下面的代码中找到。

enter image description here

MyLinearLayout.java

public class MyLinearLayout extends LinearLayout {
    private AppCompatTextView documentTitle;

    public MyLinearLayout(@NonNull Context context) {
        super(context);
        init();
    }

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

    public MyLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        documentTitle = new AppCompatTextView(this.getContext());
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        String displayString = String.format("%s - %s", "Some very, very, ", "very, very long text");

        int width = getWidth() - getPaddingEnd();
        int height = 100;

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width, height);
        documentTitle.setLayoutParams(lp);

        TextViewCompat.setAutoSizeTextTypeWithDefaults(documentTitle, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
        documentTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
        documentTitle.setTypeface(null, BOLD);
        documentTitle.setTextColor(Color.BLACK);
        documentTitle.setMaxLines(1);

        int measuredWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        int measuredHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        documentTitle.measure(measuredWidth, measuredHeight);
        // Arguments here are width and height, not measureWidth and measeredHeight.
        documentTitle.layout(0, 0, width, height);

        canvas.save();
        for (int i = 0; i < 5; i++) {
            documentTitle.setText(displayString);
            documentTitle.draw(canvas);
            displayString += " - longer - longer";
            canvas.translate(0, 150);
        }
        canvas.restore();
    }

}