android – 自动调整TextView以在画布上绘制

前端之家收集整理的这篇文章主要介绍了android – 自动调整TextView以在画布上绘制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我尝试在PDF文档上显示一些文本.此文本可能很长或很短,但应该在一行上.这就是我选择TextViewCompat.setAutoSizeTextTypeWithDefaults实用程序方法的原因,但在画布上绘图时它似乎不起作用.文本被裁剪而不是减少.

这是我的代码

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,measuredWidth,measuredHeight); 

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

我对Android内部的自定义视图感到不舒服,所以我可能在这里做错了,任何帮助都会受到赞赏.

最佳答案
Rewrite2

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

documentTitle.layout(0,measuredHeight);

这一行应该是:

documentTitle.layout(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,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();
    }

}

猜你在找的Android相关文章