Android development: drawing text on a Canvas

I have an Android project bubbling away. More details soon πŸ™‚

I’m using a SurfaceView and needed to draw some text on it. The rest of the app is mostly drawing on the SurfaceView Canvas directly, but I need some text too.

I’ve dabbled with some Android development before, and I have to admit getting the layouts to do what you want them too almost seems to suck up the majority of my development time πŸ™ This time round I got sidetracked trying to create a layout that was using a mix of TextView elements and adding my SurfaceView programmatically. This didn’t go too well, didn’t work at all, so I found a few posts like this, and worked out you need to draw directly on the canvas. Something like this does the job:

Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
this.canvas.drawText("Hello world!", 20,50, textPaint);

This code is in the middle of a game display loop, but for completeness, to be able to draw to the Canvas first you need:

SurfaceHolder surfaceHolder = this.getHolder();
if (surfaceHolder.getSurface().isValid()) {
    this.canvas = surfaceHolder.lockCanvas();
   ...
}

And then to release the Canvas after you’ve finished drawing:

this.surfaceHolder.unlockCanvasAndPost(this.canvas);

 

Ok, next up, what about custom TTF fonts? Drop the TTF font you want to use in your assets/fonts folder, and then load it up with:

Typeface typeFace = Typeface.createFromAsset(this.context.getAssets(), 
    "fonts/yourfont.ttf");

To use this font with the text snippet above, just call setTypeface() and pass it in:

textPaint.setTypeface(this.typeFace);

Ok, how about specifying a font size that’s proportional to the resolution of the user’s screen? This is an interesting question. To avoid using fixed pixel size fonts and then having them not scale appropriately for different devices with different screen resolutions, Android uses a concept called ‘scale independent pixels’, or SP. This is described in this post here. In short, defines an SP value in /res/values/dimens.xml/dimens.xml like:

<dimen name="scoreFontSize">20sp</dimen>

And then reference and set it like this:

int fontSize = getResources().getDimensionPixelSize(R.dimen.scoreFontSize);
textPaint.setTextSize(fontSize);

Done! (I’ll share more on what I’m working on in a few days πŸ™‚

Flashing Cyanogenmod 13 to a Samsung Galaxy S3

I’ve been running Cyanogenmod 12.1 on my Samsung Galaxy S3 for a year or so now, and have updated to various nightly builds when they’re available. Cyanogenmod 13 became available a while back too, so I decided I’d bump up from Lollipop up to Marshmallow.

Lollipop on this one ran pretty smooth, but it looks like it’s struggling with Marshmallow, maybe it hasn’t got the oomph.

I usually find the point I start messing with custom roms is the point where I’m looking to prolong my phone just a bit longer, so maybe at this point I’m getting close to that upgrade. Still, can’t complain, this is a nearly 4 year old phone and it’s still going strong. If I use it a lot during the day then the battery will drain within a day, but other than that, it’s been an awesome phone so far.

Android Studio – useful notes (1)

Having worked for years with Eclipse (and some time with Netbeans), I have to admit that using Android Studio (based on InteliJ) I’m fining harder that what it should be. Where’s all my key shortcut combinations!

Whenever I use Netbeans, I use the Eclipse keymappings option in the settings, so I don’t have to remember any new key shortcuts. I’m not sureΒ if Android Studio/IntelliJ has this option as well, but in the meantime, here’s some notes for useful things to remember.

Organize Imports (Eclipse: shift-Ctrl-O) / Optimize Imports: Ctrl-Opt-O

Jump to definition (Eclipse: F3) : Opt-Cmd-B

Fix error with popup suggestion: Opt-Return

 

Other useful Android development related posts:

http://stackoverflow.com/questions/17252870/how-to-show-compilation-errors-in-android-studio

http://stackoverflow.com/questions/2364811/how-do-i-write-outputs-to-the-log-in-android

 

Migrating Eclipse ADT Android project to Android Studio

It’s been a couple of years since I’ve done any Android development. In the meantime I was aware that Android Studio was coming along, but apparently now Eclipse and the ADT plugins are no longer supported, so time to migrate source from some existing apps I was working on to Android Studio.

First issue: remove paths from .classpath and project.properties files otherwise ADT library references are not resolved on import.

Next issue: “Unsupported Gradle DSL method found: ‘compile()’!” – luckily this issue and the solution is described exactly here. This is my first experience with Gradle too, so got some more learning to do πŸ™‚