Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 自动调整TextView以在画布上绘制大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

这是我的代码:@H_696_2@

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);
@H_419_10@

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

最佳答案
Rewrite2

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

documenttitle.layout(0,measuredHeight);
@H_419_10@

这一行应该是:@H_696_2@

documenttitle.layout(0,width,height);
@H_419_10@

您的代码将宽度和高度设置为某些天文值,因此文本始终“适合”.@H_696_2@

这是(主要)您的代码工作的简短演示.我使用了自定义LinearLayout,但这无关紧要.我认为只是上面的改变本身应该有效,但我可能已经做了一些其他的小调整,你可以在下面的代码中找到.@H_696_2@

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

@H_696_2@ @H_395_1@myLinearLayout.java@H_696_2@

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();="" }="" }="">@H_419_10@

大佬总结

以上是大佬教程为你收集整理的android – 自动调整TextView以在画布上绘制全部内容,希望文章能够帮你解决android – 自动调整TextView以在画布上绘制所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。