打印 Android Studio 的活动

huangapple go评论81阅读模式
英文:

Print activity of android studio

问题

I want to print my layout activity directly with a button, I don't know is necessary pass activity to PDF format or directly to print.

Thanks

英文:

I want to print my layout activity directly with a button, I don't know is neccesary pass activity to PDF format o directly to print.

Thanksenter image description here

答案1

得分: 0

要获取活动的根视图作为图像,执行以下操作:

View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

然后打印:

PrintHelper photoPrinter = new PrintHelper(this); // 假设'this'是您的活动
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("print", bitmap);
英文:

In order to get the root view of your activity as image, do this:

    View view = getWindow().getDecorView().findViewById(android.R.id.content);
    view.setDrawingCacheEnabled(true);
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View. MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

And to print:

PrintHelper photoPrinter = new PrintHelper(this); // Asume that 'this' is your activity
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("print", bitmap);

huangapple
  • 本文由 发表于 2020年1月3日 22:47:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定