我尝试显示在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定制的意见,所以我也许做错了这里,任何帮助,将不胜感激。
Rewrite2 +
你有麻烦的是以下行造成的:
documentTitle.layout(0, 0, measuredWidth, measuredHeight);
此行应该是:
documentTitle.layout(0, 0, width, height);
你的代码设置的宽度和高度,以一些天文值,因此文本始终是“适合”。
这是(主要是)你的代码工作的一个简短的演示。我使用了自定义LinearLayout
,但是这并不重要。我想,正上方的变化应通过自身工作,但我可以做出一些其他小的调整,你可以在下面的代码中找到。
my linear layout.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();
}
}