Search code examples
androidtextcanvaspixel

Can setTextSize() be density independent?


If I were to initialize a Paint, and set it's text size like so:

Paint = new Paint();
Paint.setAntiAlias(true);
Paint.setARGB(255, 255, 0, 0); 
Paint.setTextSize(screenWidth/100);

//screenWidth is the width of the screen in pixels given via display metrics.

And then draw text to the canvas like so:

String text = "Hello"
canvas.drawText(text, (screenWidth/13), (screenHeight/5), Paint);

Would the text Show up in the same relative spot the same relative size regardless of screen metrics? I ask because I only have 1 device and the Emulator doesn't run very well on my multi-core machine.

What I've been doing up until this point is simply using a bitmap with the text written over a background, but my memory usage is getting quite heavy, so I'm looking to cut down on the number of bitmaps loaded.

My other option is to save the text as a bitmap with a transparent background and overlay it on a single bitmap background. But that seems to be only half as productive, as it is actually creating 1 more bitmap, just reducing the total size of all the bitmaps stored. I also don't like this idea because i'd eventually like to take more control over the object life cycle and this will make that less effective.

Also, is there any method of adding styles to text (such as complicated fonts and color patterns besides using pre-made Drawables) so that the text can be drawn to canvas? (As cheaply as possible)


Solution

  • NVM, Solved By Poking around all day I figured out that DP units can be called from the res folder and will give a fairly uniform position of the text. and that paint is not as customization friendly as I wish.