我尝试在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,但这无关紧要.我认为只是上面的改变本身应该有效,但我可能已经做了一些其他的小调整,你可以在下面的代码中找到.
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 – 自动调整TextView以在画布上绘制全部内容,希望文章能够帮你解决android – 自动调整TextView以在画布上绘制所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)