2

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.

Loktar
  • 34,764
  • 7
  • 90
  • 104
user1
  • 599
  • 1
  • 3
  • 20
  • Welcome to StackOverflow - Please add the code you already have, what you've tried and so on to your question. Everyone here is willing to help you with specific problems but we aren't the ones who write your code. – chill0r Aug 05 '14 at 20:11

2 Answers2

6

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.

Community
  • 1
  • 1
Luke
  • 5,329
  • 2
  • 29
  • 34
  • excellent, thank you so much, was exactly what I was looking fore. Implemented it and works very nicely. thank you. – user1 Aug 05 '14 at 20:36
0

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...");
}
MSA
  • 2,502
  • 2
  • 22
  • 35