0
我一直很高興地在Android PDF類PdfDocument中生成PDF文檔。令人費解過來的「微妙」的文件後,我暗想,座標系是的PostScript(1/72" )Android上的座標和裁剪PdfDocument
private static final int A4_WIDTH = 595;
private static final int A4_HEIGHT = 842;
private static final int CM = 28;
private static final int footer_height = CM;
void startPage() {
if (currentPage != null) {
finishPage(currentPage);
}
PageInfo.Builder pageBuilder = new PageInfo.Builder(A4_WIDTH, A4_HEIGHT, page++);
Rect rect = new Rect(0, 0, A4_WIDTH, A4_HEIGHT);
rect.inset(CM, 2 * CM);
pageBuilder.setContentRect(rect);
PageInfo pageInfo = pageBuilder.create();
pagePosition = 0.0f;
pageWidth = rect.width();
pageHeight = rect.height();
currentPage = super.startPage(pageInfo);
page_count++;
if (has_footer)
putFooter();
}
這對我的Nexus 10的工作很好,但我感到相當吃驚的三星S6時的座標系看起來混亂了,所以在Nexus 10上,我把文本設置爲0,0,並且它恰好適合2cm和1cm,但是在S6上它出現了4cm和2cm的橫截面,而且底部和右側被剪裁了2cm和1cm。換句話說,剪切矩形是在正確的地方,但它出現的座標系統已經加倍抵消
我抽獎代碼如下所示:
canvas.save();
canvas.translate(0, pagePosition);
staticLayout.draw(canvas);
canvas.restore();
其中pagePosition是一個簡單的ps計數器,它通過測量staticLayout.getHeight()的結果中的文本來驅動;
需要說明的是,佈局在文字,線條圖和圖像的相當複雜的混合上沒有缺陷,只是得到了這個設備相關的缺陷。
Bug或我的大腦?
編輯:左爲的Nexus,右是S6
生產代碼:
import android.annotation.TargetApi;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.pdf.PdfDocument;
import android.support.v4.content.ContextCompat;
import uk.co.spennycycles.pocketcycletech.PocketCycleTechApp;
import uk.co.spennycycles.pocketcycletech.R;
@TargetApi(19)
public class TestReport extends PdfDocument {
private static final String TAG = SpennyPdfDocument.class.getSimpleName();
private static final int A4_WIDTH = 595;
private static final int A4_HEIGHT = 842;
private static final int CM = 28;
int pageWidth;
int pageHeight;
public void generateReport() {
PageInfo.Builder pageBuilder = new PageInfo.Builder(A4_WIDTH, A4_HEIGHT, 1);
Rect rect = new Rect(0, 0, A4_WIDTH, A4_HEIGHT);
rect.inset(CM, 2 * CM);
pageBuilder.setContentRect(rect);
PageInfo pageInfo = pageBuilder.create();
Page page = super.startPage(pageInfo);
pageWidth = rect.width();
pageHeight = rect.height();
Canvas canvas = page.getCanvas();
RectF r = new RectF((float) 0, 0, pageWidth, pageHeight);
r.inset(1.0f, 1.0f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(ContextCompat.getColor(PocketCycleTechApp.getContext(), R.color.lt_gray));
canvas.drawRect(r, paint);
paint.setColor(ContextCompat.getColor(PocketCycleTechApp.getContext(), R.color.black));
paint.setStrokeWidth(2.0f);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(r, paint);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawLine(r.left, r.top, r.right, r.bottom, paint);
canvas.drawLine(r.right, r.top, r.left, r.bottom, paint);
finishPage(page);
}
}