本文實例為大家分享了Android實現系統打印的具體代碼,供大家參考,具體內容如下
一、打印圖片
使用PrintHelper類,如:
private void doPhotoPrint() {
PrintHelper photoPrinter = new PrintHelper(getActivity());
安卓 打印機,photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.droids);
photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}
可以在應用的菜單欄中調用該方法,當printBitmap()方法調用時,Android系統的打印界面
android go支持機型?會彈出,用戶可以設置一些參數,然后進行打印或取消。
二、打印自定義文檔
1.連接到PrintManager類:
private void doPrint() {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
android 10新功能、.getSystemService(Context.PRINT_SERVICE);
// Set job name, which will be displayed in the print queue
String jobName = getActivity().getString(R.string.app_name) + " Document";
// Start a print job, passing in a PrintDocumentAdapter implementation
// to handle the generation of a print document
printManager.print(jobName, new MyPrintDocumentAdapter(getActivity()),
安卓打印服務?null); //
}
注:print函數第二個參數為繼承了抽象類PrintDocumentAdapter 的適配器類,第三個參數為 PrintAttributes對象,
可以用來設置一些打印時的屬性。
2.創建打印適配器類
打印適配器與Android系統的打印框架進行交互,處理打印的生命周期方法。打印過程主要有以下生命周期方法:
安卓手機怎么打印?onStart():當打印過程開始的時候調用;
onLayout():當用戶更改打印設置導致打印結果改變時調用,如更改紙張尺寸,紙張方向等;
onWrite():當將要打印的結果寫入到文件中時調用,該方法在每次onLayout()調用后會調用一次或多次;
onFinish():當打印過程結束時調用。
注:關鍵方法有onLayout()和onWrite(),這些方法默認都是在主線程中調用,因此如果打印過程比較耗時,應該在后臺線程中進行。
3.覆蓋onLayout()方法
android9.0,在onLayout()方法中,你的適配器需要告訴系統框架文本類型,總頁數等信息,如:
@Override
public void onLayout(PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback,
安卓 打印?Bundle metadata) {
// Create a new PdfDocument with the requested page attributes
mPdfDocument = new PrintedPdfDocument(getActivity(), newAttributes);
// Respond to cancellation request
if (cancellationSignal.isCancelled() ) {
callback.onLayoutCancelled();
android9,return;
}
// Compute the expected number of printed pages
int pages = computePageCount(newAttributes);
if (pages > 0) {
// Return print information to print framework
android,PrintDocumentInfo info = new PrintDocumentInfo
.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(pages);
.build();
// Content layout reflow is complete
android打印機,callback.onLayoutFinished(info, true);
} else {
// Otherwise report an error to the print framework
callback.onLayoutFailed("Page count calculation failed.");
}
}
安卓系統安裝打印機?注:onLayout()方法的執行有完成,取消,和失敗三種結果,你必須通過調用 PrintDocumentAdapter.LayoutResultCallback類的適當回調方法表明執行結果, onLayoutFinished()方法的布爾型參數指示布局內容是否已經改變。
onLayout()方法的主要任務就是計算在新的設置下,需要打印的頁數,如通過打印的方向決定頁數:
private int computePageCount(PrintAttributes printAttributes) {
int itemsPerPage = 4; // default item count for portrait mode
MediaSize pageSize = printAttributes.getMediaSize();
if (!pageSize.isPortrait()) {
android自帶打印服務,// Six items per page in landscape orientation
itemsPerPage = 6;
}
// Determine number of print items
int printItemCount = getPrintItemCount();
return (int) Math.ceil(printItemCount / itemsPerPage);
Android打印log、}
4.覆蓋onWrite()方法
當需要將打印結果輸出到文件中時,系統會調用onWrite()方法,該方法的參數指明要打印的頁以及結果寫入的文件,你的方法實現需要將頁面的內容寫入到一個多頁面的PDF文檔中,當這個過程完成時,需要調用onWriteFinished() 方法,如:
@Override
public void onWrite(final PageRange[] pageRanges,
final ParcelFileDescriptor destination,
安卓打印print、final CancellationSignal cancellationSignal,
final WriteResultCallback callback) {
// Iterate over each page of the document,
// check if it's in the output range.
for (int i = 0; i < totalPages; i++) {
// Check to see if this page is in the output range.
安卓手機打印功能怎么用、if (containsPage(pageRanges, i)) {
// If so, add it to writtenPagesArray. writtenPagesArray.size()
// is used to compute the next output page index.
writtenPagesArray.append(writtenPagesArray.size(), i);
PdfDocument.Page page = mPdfDocument.startPage(i);
// check for cancellation
if (cancellationSignal.isCancelled()) {
callback.onWriteCancelled();
mPdfDocument.close();
mPdfDocument = null;
return;
}
// Draw page content for printing
drawPage(page);
// Rendering is complete, so page can be finalized.
mPdfDocument.finishPage(page);
}
}
// Write PDF document to file
try {
mPdfDocument.writeTo(new FileOutputStream(
destination.getFileDescriptor()));
} catch (IOException e) {
callback.onWriteFailed(e.toString());
return;
} finally {
mPdfDocument.close();
mPdfDocument = null;
}
PageRange[] writtenPages = computeWrittenPages();
// Signal the print framework the document is complete
callback.onWriteFinished(writtenPages);
...
}
drawPage()方法實現:
private void drawPage(PdfDocument.Page page) {
Canvas canvas = page.getCanvas();
// units are in points (1/72 of an inch)
int titleBaseLine = 72;
int leftMargin = 54;
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(36);
canvas.drawText("Test Title", leftMargin, titleBaseLine, paint);
paint.setTextSize(11);
canvas.drawText("Test paragraph", leftMargin, titleBaseLine + 25, paint);
paint.setColor(Color.BLUE);
canvas.drawRect(100, 100, 172, 172, paint);
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持找一找教程網。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态