Printing Framework
android.print
The gist of it
The gist of it

PrintManager
Prerequisites


SDK platform 19



SDK build-tools 19 or later



Something to print with


A Google Cloud Print Ready...
ApiDemos


[sdk]/sampes/android-19/legacy/ApiDemos
PrintManager
PrintManager printManager = (PrintManager)
context.getSystemService(Context.PRINT_SERVICE);
PrintJob printJob...
PrintDocumentAdapter
Web pages
PrintDocumentAdapter documentAdapter =
// Directly from an existing WebView
webView.createP...
PrintDocumentAdapter
Web pages
// Create a WebView and start printing when its ready
WebView webView = new WebView(context...
PrintDocumentAdapter
Custom documents
PrintDocumentAdapter documentAdapter =
// Define a custom print adapter
new MyPrintD...
Custom Print Adapters
Events:


onStart()




Print process has begun.

onLayout()





A print setting has changed ...
Custom Print Adapters
onLayout()
public void onLayout(
final PrintAttributes oldAttributes,
final PrintAttributes newAttri...
Custom Print Adapters
onLayout()


Create a document for printing:
PrintedPdfDocument document =
new PrintedPdfDocument(c...
Custom Print Adapters
onWrite()
public void onWrite(
final PageRange[] pages,
final ParcelFileDescriptor destination,
fina...
Custom Print Adapters
onWrite()


Start writing a page:
Page page = document.startPage(pageNumber);



Draw onto the pag...
Custom Print Adapters
Caveats
If you need a screen context, remember that the printer and screen densities differ!


Obta...
PrintHelper
PrintHelper printHelper = new PrintHelper(context);
// Instruct how the content should scale
printHelper.setSc...
Happy printing!
Upcoming SlideShare
Loading in …5
×

Android Kit Kat - Printing Framework

3,227 views

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,227
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
44
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android Kit Kat - Printing Framework

  1. 1. Printing Framework android.print
  2. 2. The gist of it
  3. 3. The gist of it PrintManager
  4. 4. Prerequisites  SDK platform 19  SDK build-tools 19 or later  Something to print with  A Google Cloud Print Ready printer  A printer shared through Google Cloud Print  Print to PDF
  5. 5. ApiDemos  [sdk]/sampes/android-19/legacy/ApiDemos
  6. 6. PrintManager PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE); PrintJob printJob = printManager.print( printJobName, documentAdapter, attributes); Name for the print job (shown to the user) Default print job attributes Adapter that emits the document
  7. 7. PrintDocumentAdapter Web pages PrintDocumentAdapter documentAdapter = // Directly from an existing WebView webView.createPrintDocumentAdapter();
  8. 8. PrintDocumentAdapter Web pages // Create a WebView and start printing when its ready WebView webView = new WebView(context); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished( WebView webView, String url) { // Print to it as before PrintDocumentAdapter documentAdapter = webView.createPrintDocumentAdapter(); } }); webView.loadUrl(url);
  9. 9. PrintDocumentAdapter Custom documents PrintDocumentAdapter documentAdapter = // Define a custom print adapter new MyPrintDocumentAdapter(context);
  10. 10. Custom Print Adapters Events:  onStart()   Print process has begun. onLayout()    A print setting has changed and the layout of the pages needs to be computed. Must return expected number of pages. onWrite()   Render printed pages into a file. onFinish()  Print process has ended.
  11. 11. Custom Print Adapters onLayout() public void onLayout( final PrintAttributes oldAttributes, final PrintAttributes newAttributes, final CancellationSignal cancellationSignal, final LayoutResultCallback callback, final Bundle metadata) { CONTRACT onLayoutFinished( PrintDocumentInfo, boolean) } onLayoutCancelled() onLayoutFailed(CharSequence)
  12. 12. Custom Print Adapters onLayout()  Create a document for printing: PrintedPdfDocument document = new PrintedPdfDocument(context, newAttributes);  Compute the number of pages using PrintAttributes  Return print information to the print framework: PrintDocumentInfo info = new PrintDocumentInfo .Builder("print_output.pdf") .setContentType( PrintDocumentInfo.CONTENT_TYPE_DOCUMENT) .setPageCount(pages); .build(); callback.onLayoutFinished(info, true);  Wait for the print framework to invoke onWrite()
  13. 13. Custom Print Adapters onWrite() public void onWrite( final PageRange[] pages, final ParcelFileDescriptor destination, final CancellationSignal cancellationSignal, final WriteResultCallback callback) { CONTRACT onWriteFinished(PageRange[]) } onWriteCancelled() onWriteFailed(CharSequence)
  14. 14. Custom Print Adapters onWrite()  Start writing a page: Page page = document.startPage(pageNumber);  Draw onto the page canvas: page.getCanvas();   Note that elements are specified in points (1/72 inch) Finish writing the page: document.finishPage(page);  Inform WriteResultCallback of the PageRange[] printed
  15. 15. Custom Print Adapters Caveats If you need a screen context, remember that the printer and screen densities differ!  Obtain the printer density: printerDensity = Math.max( newAttributes.getResolution().getHorizontalDpi(), newAttributes.getResolution().getVerticalDpi());  Create a new Context to cope with a different Configuration: Configuration configuration = new Configuration(); configuration.densityDpi = printerDensity; printContext = createConfigurationContext( configuration);  Use this context for creating new views: View view = new LinearLayout(printContext); view.draw(page.getCanvas());
  16. 16. PrintHelper PrintHelper printHelper = new PrintHelper(context); // Instruct how the content should scale printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); // Get the bitmap for the ImageView's drawable Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap(); // Print the bitmap printHelper.printBitmap("Print Bitmap", bitmap);
  17. 17. Happy printing!

×