• Aucun résultat trouvé

Drawing PDF Page Content

Dans le document 2. Building Your First App (Page 168-172)

When your application prints, your application must generate a PDF document and pass it to the Android print framework for printing. You can use any PDF generation library for this purpose. This lesson shows how to use the PrintedPdfDocument class to generate PDF pages from your content.

The PrintedPdfDocument class uses a Canvas object to draw elements on an PDF page, similar to drawing on an activity layout. You can draw elements on the printed page using the Canvas draw methods. The following example code demonstrates how to draw some simple elements on a PDF document page using these methods:

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

}

When using Canvas to draw on a PDF page, elements are specified in points, which is 1/72 of an inch.

Make sure you use this unit of measure for specifying the size of elements on the page. For positioning of drawn elements, the coordinate system starts at 0,0 for the top left corner of the page.

Tip: While the Canvas object allows you to place print elements on the edge of a PDF document, many printers are not able to print to the edge of a physical piece of paper. Make sure that you account for the unprintable edges of the page when you build a print document with this class.

Building Apps with Graphics & Animation

169

Content from developer.android.com/training/building-graphics.html through their Creative Commons Attribution 2.5 license

59. Building Apps with Graphics & Animation

Content from developer.android.com/training/building-graphics.html through their Creative Commons Attribution 2.5 license

These classes teach you how to accomplish tasks with graphics that can give your app an edge on the competition. If you want to go beyond the basic user interface to create a beautiful visual experience, these classes will help you get there.

Displaying Bitmaps Efficiently

170

Content from developer.android.com/training/displaying-bitmaps/index.html through their Creative Commons Attribution 2.5 license

60. Displaying Bitmaps Efficiently

Content from developer.android.com/training/displaying-bitmaps/index.html through their Creative Commons Attribution 2.5 license

Video

DevBytes: Bitmap Allocation

Video

DevBytes: Making Apps Beautiful - Part 4 - Performance Tuning

Learn how to use common techniques to process and load Bitmap objects in a way that keeps your user interface (UI) components responsive and avoids exceeding your application memory limit. If

you're not careful, bitmaps can quickly consume your available memory budget leading to an application crash due to the dreaded exception:

java.lang.OutofMemoryError: bitmap size exceeds VM budget.

There are a number of reasons why loading bitmaps in your Android application is tricky:

• Mobile devices typically have constrained system resources. Android devices can have as little as 16MB of memory available to a single application. The Android Compatibility Definition Document (CDD), Section 3.7. Virtual Machine Compatibility gives the required minimum application memory for various screen sizes and densities. Applications should be optimized to perform under this minimum memory limit. However, keep in mind many devices are configured with higher limits.

• Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

• Android app UI’s frequently require several bitmaps to be loaded at once. Components such as ListView, GridView and ViewPager commonly include multiple bitmaps on-screen at once with many more potentially off-screen ready to show at the flick of a finger.

Lessons

Loading Large Bitmaps Efficiently

This lesson walks you through decoding large bitmaps without exceeding the per application memory limit.

Processing Bitmaps Off the UI Thread

Bitmap processing (resizing, downloading from a remote source, etc.) should never take place on the main UI thread. This lesson walks you through processing bitmaps in a background thread using AsyncTask and explains how to handle concurrency issues.

Caching Bitmaps

This lesson walks you through using a memory and disk bitmap cache to improve the responsiveness and fluidity of your UI when loading multiple bitmaps.

Managing Bitmap Memory

This lesson explains how to manage bitmap memory to maximize your app's performance.

Dependencies and prerequisites

• Android 2.1 (API Level 7) or higher

• Support Library Try it out

Download the sample BitmapFun.zip

Displaying Bitmaps Efficiently

171

Content from developer.android.com/training/displaying-bitmaps/index.html through their Creative Commons Attribution 2.5 license

Displaying Bitmaps in Your UI

This lesson brings everything together, showing you how to load multiple bitmaps into components like ViewPager and GridView using a background thread and bitmap cache.

Loading Large Bitmaps Efficiently

172

Content from developer.android.com/training/displaying-bitmaps/load-bitmap.html through their Creative Commons Attribution 2.5 license

61. Loading Large Bitmaps Efficiently

Content from developer.android.com/training/displaying-bitmaps/load-bitmap.html through their Creative Commons Attribution 2.5 license

Images come in all shapes and sizes. In many cases they are larger than required for a typical application user interface (UI). For example, the system Gallery application displays photos taken using your Android devices's camera which are typically much higher resolution than the screen density of your device.

Given that you are working with limited memory, ideally you only want to load a lower resolution

version in memory. The lower resolution version should match the size of the UI component that displays it. An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.

This lesson walks you through decoding large bitmaps without exceeding the per application memory limit by loading a smaller subsampled version in memory.

Dans le document 2. Building Your First App (Page 168-172)