There is a work around using the Android SDK, by rendering the font onto a canvas, and passing it on to a bitmap and assign that to an imageview. Anyone got a idea on how to accomplish this in B4E?
Haven't tried it myself, but that's basically how to render on a bitmap:
B4X:
Sub DrawFont(MyText As String) As Bitmap
Dim MyFont As Typeface
MyFont = Typeface.LoadFromAssets("myfont.ttf")
Dim Canvas As Canvas
Dim imgCC As Bitmap
imgCC.InitializeMutable(40dip,16dip) 'sample size
Canvas.Initialize2(imgCC)
Canvas.DrawText(MyText, 0, 0, MyFont, 16, Colors.Blue, "LEFT")
Return Canvas.Bitmap
End Sub
Please place the Sub in a code module, not in the widget service and call it from the widget service:
I'm looking for a solution to the same problem... I need to change fonts in my widget, but unfortunately this workaround doesn't seem to work for me (nothing appears in the widget), and it also causes the widget to FC, as squaremation said.
My widget with 3 custom fonts works perfect with the code in this post, which was basically the same as my own code.
I also followed Erels tutorial, everything works perfect.
The only problem I've found is that in Android 2.2/2.3, my widgets memory using just keeps going up, in Android 4.0 the memory usage stays low. I'm presuming that the garbage collection is managed better by Android 4.0. I have not tested it in Android 3.2. I've tried clearing the bitmap image after using the image but with no joy, I'm also not able to release bitmap resource afterwards.
Usually if you don't get an out of memory error then everything is fine. The garbage collector doesn't immediately releases the memory. If there is a real memory leak then eventually you will get an error.
Hello,
I don't get an 'out of memory error', but my widget service memory usage does go up by about 20mb-25mb every 24 hours, that's if I'm using a widget layout with custom fonts. My widget starts of at 5mb in size(I'm using a few libraries).
I've managed to slow down the memory usage by only creating text to image when it actually needs to and not every second. I also monitor if the screen is actually on. If the screen is not on then my widget skips the text to image routine until the screen is back on again. I integrated the screen on routine into a one second timer which I already had running in my widget. But this still does not fix the underline issue, just slows it down.
I would love to be able to keep the memory usage down to a minimum(lets say 8mb-10mb).
Anyway, I'm happy with the result so far. As after about 30 hours or so the garbage collection does its stuff and my widget memory usage goes down to 5mb again.
Usually if you don't get an out of memory error then everything is fine. The garbage collector doesn't immediately releases the memory. If there is a real memory leak then eventually you will get an error.