How to find android canvas width before instantiating the canvas object?










0














I was creating a printdocumentadapter class to generate pdf to print. I needed to automatically calculate how many pages I was going to need based off my data variable(which is just a string containing texts). So I needed to find out the width and height of my canvas in pixels or dp before I actually instantiate the canvas object in my onwrite method. Here is the class file:



public class CustomPrintDocumentAdapter extends PrintDocumentAdapter 

private Activity cur;

private int pageCount = 1;

private PrintedPdfDocument pdf;

private String data;

private int textSize = 16;

public CustomPrintDocumentAdapter(Activity current, String data)
this.cur = current;
this.data = data;


@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
pdf = new PrintedPdfDocument(cur,newAttributes);
if(cancellationSignal.isCanceled())
callback.onLayoutCancelled();


PrintDocumentInfo info = new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).setPageCount(pageCount).build();

callback.onLayoutFinished(info,true);


@Override
public void onWrite(PageRange pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
PdfDocument.Page page = pdf.startPage(1);
drawPage(page);
pdf.finishPage(page);

try
pdf.writeTo(new FileOutputStream(destination.getFileDescriptor()));
catch(IOException ie)
callback.onWriteFailed(ie.toString());
finally
pdf.close();


callback.onWriteFinished(pages);


public void drawPage(PdfDocument.Page page)

Canvas canvas = page.getCanvas();
TextPaint paint = new TextPaint();
paint.setColor(Color.BLACK);
paint.setTextSize(16);
StaticLayout.Builder.obtain(data,0,data.length(),paint,canvas.getWidth()).build().draw(canvas);

Log.d("CustomPrintDocumentAdapter","Canvas width in pixels: "+canvas.getWidth());
//prints D/CustomPrintDocumentAdapter: Canvas width in pixels: 590
//Canvas width in pixel is 590px????
//my screen is only 480 pixels wide yet the canvas displayed is smaller than my screen??





So my question is how do I find the width and height of my canvas object in pixels in my onLayout method?



I am planning on making the canvas fill the entire area on the paper (Any size).










share|improve this question


























    0














    I was creating a printdocumentadapter class to generate pdf to print. I needed to automatically calculate how many pages I was going to need based off my data variable(which is just a string containing texts). So I needed to find out the width and height of my canvas in pixels or dp before I actually instantiate the canvas object in my onwrite method. Here is the class file:



    public class CustomPrintDocumentAdapter extends PrintDocumentAdapter 

    private Activity cur;

    private int pageCount = 1;

    private PrintedPdfDocument pdf;

    private String data;

    private int textSize = 16;

    public CustomPrintDocumentAdapter(Activity current, String data)
    this.cur = current;
    this.data = data;


    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
    pdf = new PrintedPdfDocument(cur,newAttributes);
    if(cancellationSignal.isCanceled())
    callback.onLayoutCancelled();


    PrintDocumentInfo info = new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).setPageCount(pageCount).build();

    callback.onLayoutFinished(info,true);


    @Override
    public void onWrite(PageRange pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
    PdfDocument.Page page = pdf.startPage(1);
    drawPage(page);
    pdf.finishPage(page);

    try
    pdf.writeTo(new FileOutputStream(destination.getFileDescriptor()));
    catch(IOException ie)
    callback.onWriteFailed(ie.toString());
    finally
    pdf.close();


    callback.onWriteFinished(pages);


    public void drawPage(PdfDocument.Page page)

    Canvas canvas = page.getCanvas();
    TextPaint paint = new TextPaint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(16);
    StaticLayout.Builder.obtain(data,0,data.length(),paint,canvas.getWidth()).build().draw(canvas);

    Log.d("CustomPrintDocumentAdapter","Canvas width in pixels: "+canvas.getWidth());
    //prints D/CustomPrintDocumentAdapter: Canvas width in pixels: 590
    //Canvas width in pixel is 590px????
    //my screen is only 480 pixels wide yet the canvas displayed is smaller than my screen??





    So my question is how do I find the width and height of my canvas object in pixels in my onLayout method?



    I am planning on making the canvas fill the entire area on the paper (Any size).










    share|improve this question
























      0












      0








      0







      I was creating a printdocumentadapter class to generate pdf to print. I needed to automatically calculate how many pages I was going to need based off my data variable(which is just a string containing texts). So I needed to find out the width and height of my canvas in pixels or dp before I actually instantiate the canvas object in my onwrite method. Here is the class file:



      public class CustomPrintDocumentAdapter extends PrintDocumentAdapter 

      private Activity cur;

      private int pageCount = 1;

      private PrintedPdfDocument pdf;

      private String data;

      private int textSize = 16;

      public CustomPrintDocumentAdapter(Activity current, String data)
      this.cur = current;
      this.data = data;


      @Override
      public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
      pdf = new PrintedPdfDocument(cur,newAttributes);
      if(cancellationSignal.isCanceled())
      callback.onLayoutCancelled();


      PrintDocumentInfo info = new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).setPageCount(pageCount).build();

      callback.onLayoutFinished(info,true);


      @Override
      public void onWrite(PageRange pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
      PdfDocument.Page page = pdf.startPage(1);
      drawPage(page);
      pdf.finishPage(page);

      try
      pdf.writeTo(new FileOutputStream(destination.getFileDescriptor()));
      catch(IOException ie)
      callback.onWriteFailed(ie.toString());
      finally
      pdf.close();


      callback.onWriteFinished(pages);


      public void drawPage(PdfDocument.Page page)

      Canvas canvas = page.getCanvas();
      TextPaint paint = new TextPaint();
      paint.setColor(Color.BLACK);
      paint.setTextSize(16);
      StaticLayout.Builder.obtain(data,0,data.length(),paint,canvas.getWidth()).build().draw(canvas);

      Log.d("CustomPrintDocumentAdapter","Canvas width in pixels: "+canvas.getWidth());
      //prints D/CustomPrintDocumentAdapter: Canvas width in pixels: 590
      //Canvas width in pixel is 590px????
      //my screen is only 480 pixels wide yet the canvas displayed is smaller than my screen??





      So my question is how do I find the width and height of my canvas object in pixels in my onLayout method?



      I am planning on making the canvas fill the entire area on the paper (Any size).










      share|improve this question













      I was creating a printdocumentadapter class to generate pdf to print. I needed to automatically calculate how many pages I was going to need based off my data variable(which is just a string containing texts). So I needed to find out the width and height of my canvas in pixels or dp before I actually instantiate the canvas object in my onwrite method. Here is the class file:



      public class CustomPrintDocumentAdapter extends PrintDocumentAdapter 

      private Activity cur;

      private int pageCount = 1;

      private PrintedPdfDocument pdf;

      private String data;

      private int textSize = 16;

      public CustomPrintDocumentAdapter(Activity current, String data)
      this.cur = current;
      this.data = data;


      @Override
      public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
      pdf = new PrintedPdfDocument(cur,newAttributes);
      if(cancellationSignal.isCanceled())
      callback.onLayoutCancelled();


      PrintDocumentInfo info = new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).setPageCount(pageCount).build();

      callback.onLayoutFinished(info,true);


      @Override
      public void onWrite(PageRange pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
      PdfDocument.Page page = pdf.startPage(1);
      drawPage(page);
      pdf.finishPage(page);

      try
      pdf.writeTo(new FileOutputStream(destination.getFileDescriptor()));
      catch(IOException ie)
      callback.onWriteFailed(ie.toString());
      finally
      pdf.close();


      callback.onWriteFinished(pages);


      public void drawPage(PdfDocument.Page page)

      Canvas canvas = page.getCanvas();
      TextPaint paint = new TextPaint();
      paint.setColor(Color.BLACK);
      paint.setTextSize(16);
      StaticLayout.Builder.obtain(data,0,data.length(),paint,canvas.getWidth()).build().draw(canvas);

      Log.d("CustomPrintDocumentAdapter","Canvas width in pixels: "+canvas.getWidth());
      //prints D/CustomPrintDocumentAdapter: Canvas width in pixels: 590
      //Canvas width in pixel is 590px????
      //my screen is only 480 pixels wide yet the canvas displayed is smaller than my screen??





      So my question is how do I find the width and height of my canvas object in pixels in my onLayout method?



      I am planning on making the canvas fill the entire area on the paper (Any size).







      android android-canvas android-print-framework






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 20:52









      Steven

      1569




      1569






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269919%2fhow-to-find-android-canvas-width-before-instantiating-the-canvas-object%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269919%2fhow-to-find-android-canvas-width-before-instantiating-the-canvas-object%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto