user1
user1

Reputation: 709

android canvas draw text partial

on a canvas is it possible to draw text but make half of it not show up (as if it is being drawn partially off of the canvas, but actually not being off the canvas). i have an indicator on a "graph" with some text and it follows the point but I do not want the text to be drawn out of the graph portion (i am drawing other stuff outside that area).

I also have a background on the layout behind the canvas so I cannot just paint with a bitmap because that would cause some aspect ratio/sizing issues (wouldn't look good).


I have tried looking for answers all over Google, Stack overflow, and by experimentation with no avail. On Google I found many interesting things about drawing text on an android canvas but nothing that I was looking fore, I am pretty stumped, and I am starting to think that what I want is not even possible, and i may need to draw the text custom with points or figure out an alternative that looks just as good.

Upvotes: 2

Views: 1607

Answers (2)

MSA
MSA

Reputation: 2482

The simplest solution that popped in my mind, would be to cut using substring the text that you want to dispaly.

Example:

if(MyString >5){
   canvas.drawText("VeryLongTe...");
}

Upvotes: 0

Luke
Luke

Reputation: 5564

It's absolutely possible. Probably the fastest is to use clipRect to limit drawing to your graph portion. See this answer: Using clipRect - explanation

The reference on the various forms of clipRect is here: http://developer.android.com/reference/android/graphics/Canvas.html#clipRect(android.graphics.Rect, android.graphics.Region.Op)

If I recall, the whole operation will look something like:

yourCanves.save()
yourCanvas.clipRect(...)
yourCanvas.drawText(...)
yourCanvas.restore()

The restore() call serves to undo the clipRect so you can draw outside it for later drawing.

Upvotes: 6

Related Questions