Android如何實(shí)現(xiàn)系統(tǒng)打印功能-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)Android如何實(shí)現(xiàn)系統(tǒng)打印功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)公司主營沈陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),沈陽h5成都小程序開發(fā)搭建,沈陽網(wǎng)站營銷推廣歡迎沈陽等地區(qū)企業(yè)咨詢

具體內(nèi)容如下

一、打印圖片

使用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);
}

可以在應(yīng)用的菜單欄中調(diào)用該方法,當(dāng)printBitmap()方法調(diào)用時,Android系統(tǒng)的打印界面
會彈出,用戶可以設(shè)置一些參數(shù),然后進(jìn)行打印或取消。

二、打印自定義文檔

1.連接到PrintManager類:

private void doPrint() {
 // Get a PrintManager instance
 PrintManager printManager = (PrintManager) getActivity()
   .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函數(shù)第二個參數(shù)為繼承了抽象類PrintDocumentAdapter 的適配器類,第三個參數(shù)為 PrintAttributes對象,

可以用來設(shè)置一些打印時的屬性。

2.創(chuàng)建打印適配器類

打印適配器與Android系統(tǒng)的打印框架進(jìn)行交互,處理打印的生命周期方法。打印過程主要有以下生命周期方法:

  • onStart():當(dāng)打印過程開始的時候調(diào)用;

  • onLayout():當(dāng)用戶更改打印設(shè)置導(dǎo)致打印結(jié)果改變時調(diào)用,如更改紙張尺寸,紙張方向等;

  • onWrite():當(dāng)將要打印的結(jié)果寫入到文件中時調(diào)用,該方法在每次onLayout()調(diào)用后會調(diào)用一次或多次;

  • onFinish():當(dāng)打印過程結(jié)束時調(diào)用。

注:關(guān)鍵方法有onLayout()和onWrite(),這些方法默認(rèn)都是在主線程中調(diào)用,因此如果打印過程比較耗時,應(yīng)該在后臺線程中進(jìn)行。

3.覆蓋onLayout()方法

在onLayout()方法中,你的適配器需要告訴系統(tǒng)框架文本類型,總頁數(shù)等信息,如:

@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();
  return;
 }
 
 // Compute the expected number of printed pages
 int pages = computePageCount(newAttributes);
 
 if (pages > 0) {
  // Return print information to print framework
  PrintDocumentInfo info = new PrintDocumentInfo
    .Builder("print_output.pdf")
    .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
    .setPageCount(pages);
    .build();
  // Content layout reflow is complete
  callback.onLayoutFinished(info, true);
 } else {
  // Otherwise report an error to the print framework
  callback.onLayoutFailed("Page count calculation failed.");
 }
}

注:onLayout()方法的執(zhí)行有完成,取消,和失敗三種結(jié)果,你必須通過調(diào)用 PrintDocumentAdapter.LayoutResultCallback類的適當(dāng)回調(diào)方法表明執(zhí)行結(jié)果, onLayoutFinished()方法的布爾型參數(shù)指示布局內(nèi)容是否已經(jīng)改變。

onLayout()方法的主要任務(wù)就是計(jì)算在新的設(shè)置下,需要打印的頁數(shù),如通過打印的方向決定頁數(shù):
private int computePageCount(PrintAttributes printAttributes) {
 int itemsPerPage = 4; // default item count for portrait mode
 
 MediaSize pageSize = printAttributes.getMediaSize();
 if (!pageSize.isPortrait()) {
  // Six items per page in landscape orientation
  itemsPerPage = 6;
 }
 
 // Determine number of print items
 int printItemCount = getPrintItemCount();
 
 return (int) Math.ceil(printItemCount / itemsPerPage);
}

4.覆蓋onWrite()方法

當(dāng)需要將打印結(jié)果輸出到文件中時,系統(tǒng)會調(diào)用onWrite()方法,該方法的參數(shù)指明要打印的頁以及結(jié)果寫入的文件,你的方法實(shí)現(xiàn)需要將頁面的內(nèi)容寫入到一個多頁面的PDF文檔中,當(dāng)這個過程完成時,需要調(diào)用onWriteFinished() 方法,如:

@Override
public void onWrite(final PageRange[] pageRanges,
     final ParcelFileDescriptor destination,
     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()方法實(shí)現(xiàn):

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);
}

感謝各位的閱讀!關(guān)于“Android如何實(shí)現(xiàn)系統(tǒng)打印功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)站題目:Android如何實(shí)現(xiàn)系統(tǒng)打印功能-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://m.kartarina.com/article44/cddihe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器動態(tài)網(wǎng)站網(wǎng)站收錄關(guān)鍵詞優(yōu)化虛擬主機(jī)小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
主站蜘蛛池模板: 亚洲精品无码永久在线观看男男 | 无码天堂亚洲国产AV| 亚洲Av无码精品色午夜| 天堂Av无码Av一区二区三区| 亚洲AV无码乱码国产麻豆穿越| 亚洲国产成人无码AV在线影院 | 免费a级毛片无码av| 国模吧无码一区二区三区| 无码精品不卡一区二区三区| 色欲AV永久无码精品无码| 东京热人妻无码一区二区av| 日韩精品无码人成视频手机| 亚洲欧洲av综合色无码| 亚洲爆乳无码一区二区三区| 免费A级毛片无码A| JAVA性无码HD中文| 无码一区二区波多野结衣播放搜索 | 亚洲国产精品无码久久久蜜芽| 最新无码专区视频在线| 亚洲av永久无码精品网站| 亚洲高清无码综合性爱视频| 亚洲一级Av无码毛片久久精品| 在线看片无码永久免费视频| 欧洲精品久久久av无码电影| 无码人妻AV免费一区二区三区 | 亚洲AV无码一区二区三区性色| 无码人妻丰满熟妇精品区| 国产成人无码久久久精品一| 成年免费a级毛片免费看无码| 国产精品无码一区二区三区不卡 | 日韩AV无码不卡网站| 久久99久久无码毛片一区二区| 五十路熟妇高熟无码视频 | 亚洲色偷拍区另类无码专区| 毛片亚洲AV无码精品国产午夜| 亚洲av中文无码字幕色不卡| 亚洲AV无码专区国产乱码不卡 | 精品人妻少妇嫩草AV无码专区| 日韩AV无码不卡网站| 国产成人无码免费看片软件 | 天堂无码在线观看|