Search code examples
androidcanvasandroid-custom-viewcustom-view

android canvas.drawText in custom view


I have a custom view in which i want to drawText with these parameters.

int stopY = 500;

// ...

Log.info("Drawing line " + line + " at " + String.valueOf(stopY));
canvas.drawText(line, 40, stopY, Paint);

i am having a array of text and i am drawing

 for (int i = startLineNumber; i < (startLineNumber + linesVisible); i++, screenDisplayLine++)
        {
            if (i >= totalLines)
            {
                break;
            }
            int startY = (int) (i*lineSpacing + (screenDisplayLine * lineSpacing));
            int stopY = startY + lineSpacing;
       canvas.drawText(line, 40, stopY, Paint);//stop y increments by 20 every time i use 
    }

it works in straightly but when i needs startnumber as 50

that mean

 stopy = stopY+(linenumber*20)//(stopY + (50 * 20))

but it fails in this case

any help

I can see the correct values in the log through LogCat, but the text is not drawn. Can anyone help me out?


Solution

  • Thanks to both of your answers; with that I figured out the answer

    int stopY = 500;
    
    scrollTo(0,stopY);
    canvas.drawText(line, 0, stopY, Paint);
    

    This works fine. As you said it goes to out-off screen size I used

    scrollTo(0,stopY);
    

    So that it scrolls to that point in the screen and when I used

    canvas.drawText(line, 0, stopY, Paint);
    

    It has drawn it there without any problem.