【移动应用开发技术】Android如何实现系统打印功能_第1页
【移动应用开发技术】Android如何实现系统打印功能_第2页
【移动应用开发技术】Android如何实现系统打印功能_第3页
【移动应用开发技术】Android如何实现系统打印功能_第4页
【移动应用开发技术】Android如何实现系统打印功能_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】Android如何实现系统打印功能

这篇文章给大家分享的是有关Android如何实现系统打印功能的内容。在下觉得挺实用的,因此分享给大家做个参考,一起跟随在下过来看看吧。具体内容如下一、打印图片使用PrintHelper类,如: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);

}可以在应用的菜单栏中调用该方法,当printBitmap()方法调用时,Android系统的打印界面会弹出,用户可以设置一些参数,然后进行打印或取消。二、打印自定义文档1.连接到PrintManager类:private

void

doPrint()

{

//

Get

a

PrintManager

instance

PrintManager

printManager

=

(PrintManager)

getActivity()

.getSystemService(Context.PRINT_SERVICE);

//

Set

job

name,

which

will

be

displayed

in

the

print

queue

String

jobName

=

getActivity().getString(R.string.app_name)

+

"

Document";

//

Start

a

print

job,

passing

in

a

PrintDocumentAdapter

implementation

//

to

handle

the

generation

of

a

print

document

printManager.print(jobName,

new

MyPrintDocumentAdapter(getActivity()),

null);

//

}注:print函数第二个参数为继承了抽象类PrintDocumentAdapter的适配器类,第三个参数为PrintAttributes对象,可以用来设置一些打印时的属性。2.创建打印适配器类打印适配器与Android系统的打印框架进行交互,处理打印的生命周期方法。打印过程主要有以下生命周期方法:onStart():当打印过程开始的时候调用;onLayout():当用户更改打印设置导致打印结果改变时调用,如更改纸张尺寸,纸张方向等;onWrite():当将要打印的结果写入到文件中时调用,该方法在每次onLayout()调用后会调用一次或多次;onFinish():当打印过程结束时调用。注:关键方法有onLayout()和onWrite(),这些方法默认都是在主线程中调用,因此如果打印过程比较耗时,应该在后台线程中进行。3.覆盖onLayout()方法在onLayout()方法中,你的适配器需要告诉系统框架文本类型,总页数等信息,如:@Override

public

void

onLayout(PrintAttributes

oldAttributes,

PrintAttributes

newAttributes,

CancellationSignal

cancellationSignal,

LayoutResultCallback

callback,

Bundle

metadata)

{

//

Create

a

new

PdfDocument

with

the

requested

page

attributes

mPdfDocument

=

new

PrintedPdfDocument(getActivity(),

newAttributes);

//

Respond

to

cancellation

request

if

(cancellationSignal.isCancelled()

)

{

callback.onLayoutCancelled();

return;

}

//

Compute

the

expected

number

of

printed

pages

int

pages

=

computePageCount(newAttributes);

if

(pages

>

0)

{

//

Return

print

information

to

print

framework

PrintDocumentInfo

info

=

new

PrintDocumentInfo

.Builder("print_output.pdf")

.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)

.setPageCount(pages);

.build();

//

Content

layout

reflow

is

complete

callback.onLayoutFinished(info,

true);

}

else

{

//

Otherwise

report

an

error

to

the

print

framework

callback.onLayoutFailed("Page

count

calculation

failed.");

}

}注:onLayout()方法的执行有完成,取消,和失败三种结果,你必须通过调用PrintDocumentAdapter.LayoutResultCallback类的适当回调方法表明执行结果,onLayoutFinished()方法的布尔型参数指示布局内容是否已经改变。onLayout()方法的主要任务就是计算在新的设置下,需要打印的页数,如通过打印的方向决定页数:

private

int

computePageCount(PrintAttributes

printAttributes)

{

int

itemsPerPage

=

4;

//

default

item

count

for

portrait

mode

MediaSize

pageSize

=

printAttributes.getMediaSize();

if

(!pageSize.isPortrait())

{

//

Six

items

per

page

in

landscape

orientation

itemsPerPage

=

6;

}

//

Determine

number

of

print

items

int

printItemCount

=

getPrintItemCount();

return

(int)

Math.ceil(printItemCount

/

itemsPerPage);

}4.覆盖onWrite()方法当需要将打印结果输出到文件中时,系统会调用onWrite()方法,该方法的参数指明要打印的页以及结果写入的文件,你的方法实现需要将页面的内容写入到一个多页面的PDF文档中,当这个过程完成时,需要调用onWriteFinished()方法,如:@Override

public

void

onWrite(final

PageRange[]

pageRanges,

final

ParcelFileDescriptor

destination,

final

CancellationSignal

cancellationSignal,

final

WriteResultCallback

callback)

{

//

Iterate

over

each

page

of

the

document,

//

check

if

it's

in

the

output

range.

for

(int

i

=

0;

i

<

totalPages;

i++)

{

//

Check

to

see

if

this

page

is

in

the

output

range.

if

(containsPage(pageRanges,

i))

{

//

If

so,

add

it

to

writtenPagesArray.

writtenPagesArray.size()

//

is

used

to

compute

the

next

output

page

index.

writtenPagesArray.append(writtenPagesArray.size(),

i);

PdfDocument.Page

page

=

mPdfDocument.startPage(i);

//

check

for

cancellation

if

(cancellationSignal.isCancelled())

{

callback.onWriteCancelled();

mPdfDocument.close();

mPdfDocument

=

null;

return;

}

//

Draw

page

content

for

printing

drawPage(page);

//

Rendering

is

complete,

so

page

can

be

finalized.

mPdfDocument.finishPage(page);

}

}

//

Write

PDF

document

to

file

try

{

mPdfDocument.writeTo(new

FileOutputStream(

destination.getFileDescriptor()));

}

catch

(IOException

e)

{

callback.onWriteFailed(e.toString());

return;

}

finally

{

mPdfDocument.close();

mPdfDocument

=

null;

}

PageRange[]

writtenPages

=

computeWrittenPages();

//

Signal

the

print

framework

the

document

is

complete

callback.onWriteFinished(writtenPages);

...

}drawPage()方法实现: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.setTe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论