הדפסת תמונות
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
צילום ושיתוף של תמונות הם אחד השימושים הנפוצים ביותר במכשירים ניידים. אם הבקשה שלכם
מצלם תמונות או מציג אותן או מאפשר למשתמשים לשתף תמונות, כדאי לשקול לאפשר הדפסה
של התמונות האלה באפליקציה שלכם. ספריית התמיכה של Android מספקת פונקציה נוחה להפעלה של הדפסת תמונות באמצעות
כמות מינימלית של קוד וקבוצה פשוטה של אפשרויות לפריסת הדפסה.
בשיעור הזה תלמדו איך להדפיס תמונה באמצעות הכיתה PrintHelper
בספריית התמיכה בגרסה 4.
הדפסת תמונה
הכיתה PrintHelper
של ספריית התמיכה של Android מספקת
דרך פשוטה להדפיס תמונות. לכיתה יש אפשרות פריסה אחת, setScaleMode()
,
שמאפשר להדפיס באמצעות אחת משתי אפשרויות:
SCALE_MODE_FIT
– הזה
מגדיר את גודל התמונה כך שהתמונה כולה תוצג באזור הניתן להדפסה של הדף.
SCALE_MODE_FILL
– הזה
אפשרות משנה את גודל התמונה כך שתמלא את כל האזור הניתן להדפסה בדף. בחירה באפשרות הזו
פירושו שחלק מהקצה העליון והתחתון של התמונה, או מהקצה השמאלי והימני של התמונה
לא הודפסו. האפשרות הזו היא ערך ברירת המחדל אם לא מגדירים מצב קנה מידה.
שתי אפשרויות ההתאמה לעומס (setScaleMode()
) שומרות על יחס הגובה-רוחב הקיים של התמונה ללא שינוי. הקוד לדוגמה הבא
מראים איך ליצור מופע של המחלקה PrintHelper
, להגדיר
אפשרות של שינוי קנה מידה, ולהתחיל את תהליך ההדפסה:
Kotlin
private fun doPhotoPrint() {
activity?.also { context ->
PrintHelper(context).apply {
scaleMode = PrintHelper.SCALE_MODE_FIT
}.also { printHelper ->
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)
printHelper.printBitmap("droids.jpg - test print", bitmap)
}
}
}
Java
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);
}
אפשר לקרוא לשיטה הזו כפעולה של אפשרות בתפריט. שימו לב שהאפשרויות בתפריט לגבי פעולות
לא תמיד נתמך (כגון הדפסה) יש למקם בתפריט האפשרויות הנוספות. לקבלת מידע נוסף
מידע נוסף, ראו את העיצוב של סרגל הפעולות
מותאמת אישית.
אחרי שה-method printBitmap()
היא
לא נדרשת כל פעולה נוספת מצידך. ממשק המשתמש להדפסה של Android
שמוצגת, וכך המשתמש יכול לבחור מדפסת ואפשרויות הדפסה. לאחר מכן המשתמש יכול להדפיס את
תמונה או לבטל את הפעולה. אם המשתמש בוחר להדפיס את התמונה, נוצרת משימת הדפסה
התראת ההדפסה מופיעה בסרגל המערכת.
אם אתם רוצים לכלול בתדפיסים תוכן נוסף מעבר לתמונה, תצטרכו
ליצור מסמך להדפסה. לקבלת מידע על יצירת מסמכים להדפסה, אפשר לעיין ב
הדפסת מסמך HTML או
הדפסת מסמך מותאם אישית
שיעורים.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-27 (שעון UTC)."],[],[],null,["# Printing photos\n\nTaking and sharing photos is one of the most popular uses for mobile devices. If your application\ntakes photos, displays them, or allows users to share images, you should consider enabling printing\nof those images in your application. The [Android Support Library](/tools/support-library) provides a convenient function for enabling image printing using a\nminimal amount of code and simple set of print layout options.\n\nThis lesson shows you how to print an image using the v4 support library [PrintHelper](/reference/androidx/print/PrintHelper) class.\n\nPrint an image\n--------------\n\nThe Android Support Library [PrintHelper](/reference/androidx/print/PrintHelper) class provides\na simple way to print images. The class has a single layout option, [setScaleMode()](/reference/androidx/print/PrintHelper#setScaleMode(int)),\nwhich allows you to print with one of two options:\n\n- [SCALE_MODE_FIT](/reference/androidx/print/PrintHelper#SCALE_MODE_FIT) - This option sizes the image so that the whole image is shown within the printable area of the page.\n- [SCALE_MODE_FILL](/reference/androidx/print/PrintHelper#SCALE_MODE_FILL) - This option scales the image so that it fills the entire printable area of the page. Choosing this setting means that some portion of the top and bottom, or left and right edges of the image is not printed. This option is the default value if you do not set a scale mode.\n\nBoth scaling options for [setScaleMode()](/reference/androidx/print/PrintHelper#setScaleMode(int)) keep the existing aspect ratio of the image intact. The following code example\nshows how to create an instance of the [PrintHelper](/reference/androidx/print/PrintHelper) class, set the\nscaling option, and start the printing process: \n\n### Kotlin\n\n```kotlin\nprivate fun doPhotoPrint() {\n activity?.also { context -\u003e\n PrintHelper(context).apply {\n scaleMode = PrintHelper.SCALE_MODE_FIT\n }.also { printHelper -\u003e\n val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)\n printHelper.printBitmap(\"droids.jpg - test print\", bitmap)\n }\n }\n}\n```\n\n### Java\n\n```java\nprivate void doPhotoPrint() {\n PrintHelper photoPrinter = new PrintHelper(getActivity());\n photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);\n Bitmap bitmap = BitmapFactory.decodeResource(getResources(),\n R.drawable.droids);\n photoPrinter.printBitmap(\"droids.jpg - test print\", bitmap);\n}\n```\n\n\nThis method can be called as the action for a menu item. Note that menu items for actions that are\nnot always supported (such as printing) should be placed in the overflow menu. For more\ninformation, see the [Action Bar](/design/patterns/actionbar) design\nguide.\n\nAfter the [printBitmap()](/reference/androidx/print/PrintHelper#printBitmap(java.lang.String, android.graphics.Bitmap)) method is\ncalled, no further action from your application is required. The Android print user interface\nappears, allowing the user to select a printer and printing options. The user can then print the\nimage or cancel the action. If the user chooses to print the image, a print job is created and a\nprinting notification appears in the system bar.\n\nIf you want to include additional content in your printouts beyond just an image, you must\nconstruct a print document. For information on creating documents for printing, see the\n[Printing an HTML document](/training/printing/html-docs) or\n[Printing a custom document](/training/printing/custom-docs)\nlessons."]]