Android Kit Kat - Printing Framework
-
Upload
paul-lammertsma -
Category
Technology
-
view
1.701 -
download
2
Transcript of Android Kit Kat - Printing Framework
![Page 1: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/1.jpg)
Printing Frameworkandroid.print
![Page 2: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/2.jpg)
The gist of it
![Page 3: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/3.jpg)
The gist of it
PrintManager
![Page 4: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/4.jpg)
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
![Page 5: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/5.jpg)
ApiDemos
[sdk]/sampes/android-19/legacy/ApiDemos
![Page 6: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/6.jpg)
PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
PrintJob printJob = printManager.print( printJobName, documentAdapter, attributes);
PrintManager
Name for the print job(shown to the user)
Adapter that emits the document
Default print job attributes
![Page 7: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/7.jpg)
PrintDocumentAdapter documentAdapter =
// Directly from an existing WebView webView.createPrintDocumentAdapter();
PrintDocumentAdapterWeb pages
![Page 8: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/8.jpg)
// Create a WebView and start printing when its readyWebView 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);
PrintDocumentAdapterWeb pages
![Page 9: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/9.jpg)
PrintDocumentAdapter documentAdapter =
// Define a custom print adapter
new MyPrintDocumentAdapter(context);
PrintDocumentAdapterCustom documents
![Page 10: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/10.jpg)
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.
![Page 11: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/11.jpg)
Custom Print AdaptersonLayout()
public void onLayout( final PrintAttributes oldAttributes, final PrintAttributes newAttributes, final CancellationSignal cancellationSignal, final LayoutResultCallback callback, final Bundle metadata) {
}
onLayoutFinished( PrintDocumentInfo, boolean)
onLayoutFailed(CharSequence)
onLayoutCancelled()
CONTRACT
![Page 12: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/12.jpg)
Custom Print AdaptersonLayout()
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()
![Page 13: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/13.jpg)
Custom Print AdaptersonWrite()
public void onWrite( final PageRange[] pages, final ParcelFileDescriptor destination, final CancellationSignal cancellationSignal, final WriteResultCallback callback) {
}
onWriteFinished(PageRange[])
onWriteFailed(CharSequence)
onWriteCancelled()
CONTRACT
![Page 14: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/14.jpg)
Custom Print AdaptersonWrite()
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
![Page 15: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/15.jpg)
Custom Print AdaptersCaveats
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());
![Page 16: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/16.jpg)
PrintHelper printHelper = new PrintHelper(context);
// Instruct how the content should scaleprintHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
// Get the bitmap for the ImageView's drawableBitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
// Print the bitmapprintHelper.printBitmap("Print Bitmap", bitmap);
PrintHelper
![Page 17: Android Kit Kat - Printing Framework](https://reader035.fdocuments.net/reader035/viewer/2022062307/5583d91dd8b42af76c8b4cd4/html5/thumbnails/17.jpg)
Happy printing!