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

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

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

这是我的代码

  1. PageInfo startingPageInfo = new Builder(STANDARD_PDF_PAGE_WIDTH,STANDARD_PDF_PAGE_HEIGHT,1).create();
  2. final Page startingPage = document.startPage(startingPageInfo);
  3. Canvas canvas = startingPage.getCanvas();
  4. canvas.save();
  5. final TextView documentTitle = new TextView(this);
  6. TextViewCompat.setAutoSizeTextTypeWithDefaults(documentTitle,TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
  7. documentTitle.setLines(1);
  8. documentTitle.setText(String.format("%s - %s",category.getName(),period));
  9. documentTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,56);
  10. documentTitle.setTypeface(null,BOLD);
  11. documentTitle.setTextColor(Color.BLACK);
  12. int measuredWidth = MeasureSpec.makeMeasureSpec(canvas.getWidth() - 100,MeasureSpec.EXACTLY);
  13. int measuredHeight = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
  14. documentTitle.measure(measuredWidth,measuredHeight);
  15. documentTitle.layout(0,measuredWidth,measuredHeight);
  16. canvas.translate(titleDx,titleDy);
  17. documentTitle.draw(canvas);

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

最佳答案
Rewrite2

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

  1. documentTitle.layout(0,measuredHeight);

这一行应该是:

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

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

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

enter image description here

MyLinearLayout.java

  1. public class MyLinearLayout extends LinearLayout {
  2. private AppCompatTextView documentTitle;
  3. public MyLinearLayout(@NonNull Context context) {
  4. super(context);
  5. init();
  6. }
  7. public MyLinearLayout(@NonNull Context context,@Nullable AttributeSet attrs) {
  8. super(context,attrs);
  9. init();
  10. }
  11. public MyLinearLayout(@NonNull Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
  12. super(context,attrs,defStyleAttr);
  13. init();
  14. }
  15. private void init() {
  16. documentTitle = new AppCompatTextView(this.getContext());
  17. }
  18. @Override
  19. public void onDraw(Canvas canvas) {
  20. super.onDraw(canvas);
  21. String displayString = String.format("%s - %s","Some very,very,","very,very long text");
  22. int width = getWidth() - getPaddingEnd();
  23. int height = 100;
  24. ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width,height);
  25. documentTitle.setLayoutParams(lp);
  26. TextViewCompat.setAutoSizeTextTypeWithDefaults(documentTitle,TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
  27. documentTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,56);
  28. documentTitle.setTypeface(null,BOLD);
  29. documentTitle.setTextColor(Color.BLACK);
  30. documentTitle.setMaxLines(1);
  31. int measuredWidth = MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY);
  32. int measuredHeight = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);
  33. documentTitle.measure(measuredWidth,measuredHeight);
  34. // Arguments here are width and height,not measureWidth and measeredHeight.
  35. documentTitle.layout(0,height);
  36. canvas.save();
  37. for (int i = 0; i < 5; i++) {
  38. documentTitle.setText(displayString);
  39. documentTitle.draw(canvas);
  40. displayString += " - longer - longer";
  41. canvas.translate(0,150);
  42. }
  43. canvas.restore();
  44. }
  45. }

猜你在找的Android相关文章