In ảnh
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Chụp và chia sẻ ảnh là một trong những cách phổ biến nhất dành cho thiết bị di động. Nếu ứng dụng của bạn
chụp ảnh, hiển thị ảnh hoặc cho phép người dùng chia sẻ hình ảnh, bạn nên cân nhắc việc bật tính năng in
của những hình ảnh đó trong ứng dụng của bạn. Thư viện hỗ trợ Android cung cấp một chức năng thuận tiện cho phép in hình ảnh bằng
số lượng mã tối thiểu và tập hợp các tuỳ chọn bố cục in đơn giản.
Bài học này sẽ hướng dẫn bạn cách in hình ảnh bằng lớp PrintHelper
của thư viện hỗ trợ v4.
In hình ảnh
Lớp PrintHelper
của Thư viện hỗ trợ Android cung cấp
giúp bạn dễ dàng in hình ảnh. Lớp có một tuỳ chọn bố cục duy nhất là setScaleMode()
,
cho phép bạn in bằng một trong hai tuỳ chọn:
SCALE_MODE_FIT
– Nội dung này
tuỳ chọn định cỡ hình ảnh sao cho toàn bộ hình ảnh hiển thị trong vùng in được trên trang.
SCALE_MODE_FILL
– Nội dung này
điều chỉnh tỷ lệ hình ảnh để hình ảnh lấp đầy toàn bộ vùng in được trên trang. Đang chọn mục này
nghĩa là một số phần ở phần trên cùng và dưới cùng hoặc cạnh trái và phải của hình ảnh
chưa được in. Tuỳ chọn này là giá trị mặc định nếu bạn không đặt chế độ tỷ lệ.
Cả hai tuỳ chọn điều chỉnh tỷ lệ cho setScaleMode()
đều giữ nguyên tỷ lệ khung hình hiện tại của hình ảnh. Ví dụ về mã sau đây
cho biết cách tạo một thực thể của lớp PrintHelper
, đặt giá trị
điều chỉnh tỷ lệ và bắt đầu quá trình in:
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);
}
Phương thức này có thể được gọi dưới dạng thao tác cho một mục trong trình đơn. Xin lưu ý rằng các mục trong trình đơn cho những thao tác
không phải lúc nào cũng được hỗ trợ (chẳng hạn như in) phải được đặt trong trình đơn mục bổ sung. Để biết thêm
thông tin, hãy xem thiết kế của Thanh hành động
của chúng tôi.
Sau phương thức printBitmap()
là
, bạn không cần làm gì thêm từ ứng dụng của mình. Giao diện người dùng của Android Print
xuất hiện, cho phép người dùng chọn máy in và các tuỳ chọn in. Sau đó, người dùng có thể in
hoặc huỷ thao tác. Nếu người dùng chọn in hình ảnh, lệnh in sẽ được tạo và
thông báo in sẽ xuất hiện trên thanh hệ thống.
Nếu bạn muốn đưa nội dung khác vào bản in ngoài hình ảnh, bạn phải
tạo tài liệu in. Để biết thông tin về cách tạo tài liệu để in, hãy xem
In tài liệu HTML hoặc
In một tài liệu tuỳ chỉnh
bài học.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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."]]