Canvas - mirrorize/invert string

Penko

Active Member
Licensed User
Longtime User
Hey guys!

I am having a very weird problem. I can't think of a way to flip my string in the way I want.

I am not sure if you will understand without posting an image here but I will try. Assuming I have the following string(to simplify, I will use a number):
123. What I want is NOT to get 321 but to get this string mirrored.


What I've done so far:

B4X:
Dim c As Canvas
c.Initialize(Activity)
c.DrawTextRotated(alll arguments......., 180)

Unfortunately, this flips my text around the wrong axis(Y). I have to flip it around the X axis(towards the width of the Text) in order to really invert it.

I suppose that's pretty easy to implement but probably I am too tired from today's work.

Can I do it with drawText only, without creating an image?
 
Last edited:

terryn

Member
Licensed User
Longtime User
Can I do it with drawText only said:
Penko,
There are several ways to do this. You can make your own font or have bitmaps of all the charactors you need to display, or you can DrawText to a mutable bitmap and DrawBitmapFlipped to a canvas.

Obviously DrawBitmapFlipped is the easiest way to go.

Terry
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Terry, it was a little bit of external help that I needed in terms of receiving a direction where to look.

I needed to initialize a second Canvas which is related to the bitmap. My first canvas is related to the activity.

My question is, would that create big memory overhead at some point?

Thanks to you and several other threads I found, I could assemble this code. It is not quite a common task, but someone else may need it: (P.S Sorry for the lame variable naming, it was just to test, now I will rename them for production).

B4X:
Dim b As Bitmap
      b.InitializeMutable(100%x, 100%y)
      
      Dim c2 As Canvas
      c2.Initialize2(b)
         c2.DrawTextRotated(str, xCoordinate, yCoordinate, TypeFaceType, FontSize, FontColor, "CENTER", angle)
            
      Dim rec As Rect
      rec.Initialize(0, 0, 100%x, 100%y)
      c.DrawBitmapFlipped(b, Null, rec, False, True)    ' my Main Canvas draws the flipped bitmap to the activity

Now I am looking for optimization suggestions, if any.
 
Upvote 0

terryn

Member
Licensed User
Longtime User
Penko,
The memory size of the bitmap + canvas together is quite large but most phones/pads seem to handle much more than this. The garbage collector does a very good job.

Quite a lot of memory can be be saved by reducing the bitmap/canvas size to only what you actualy need.


Terry
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Hello Terry!

I've further optimized but minimizing the repeated initializations. I just moved them out of my repeated function and initialize them once in Activity_Resume.

To test this out, I left my application work the whole night for me(showing random flipped strings at a timer of 300 ms) and when I woke up, it was still working. So I assume that's fair enough.

Thank you for the spent time!
 
Upvote 0
Top