Category Archives: Mobile

Mobile application development using .NET and the Droid Java experience.

Drawing with the Canvas object in Android

When writing an Android game one of the first things I had to start looking at is the methods provided for drawing. Being that I had no real purpose for developing a 3D game, I decided to go 2D. There are many things that a bitmap can do for you in regards to saving you development time, but the game platform that I am pursuing next did much better with mathematical drawing.

This brought me to searching out the object that I would use to display colorful shapes that I chose at specific window positions. The first object I have come across to do this is the Canvas. A Canvas can be accessed by overriding the OnDraw method of the Android View object. A View is an object that you place in your layout to indicate UI functionality. While there are many methods in the Canvas that work well for drawing, I focused in on two of them which were drawLine and drawRectangle.

In my example I went through a few iterations to figure out screen width and what I should do with it, but ultimately I started focusing in on using a percentage of the screen with as opposed to using hard coded numbers, as it scales better on landscape mode if you go back and forth between portrait. Ultimately I came up with a method that looks like this to draw a block on my game:

GamePieceBlock block = m_BoardBlocks.get(i);
blockLeftSide = leftSide + (width / m_PiecesWide) * block.getXPosition();
blockRightSide = blockLeftSide + (width / m_PiecesWide);
Paint pnt = new Paint();

blockBottomSide = bottomSide - (height / m_PiecesTall) * block.getYPosition();
blockTopSide = blockBottomSide - (height / m_PiecesTall);

m_Canvas.drawRect(blockLeftSide, blockTopSide, blockRightSide, blockBottomSide, pnt);

This method draws a solid block of color at the position that I calculated.  To get the m_Canvas instance I set it in my onDraw method and then call down to my game methods to draw the object on the canvas.  The other method that I use is the drawLine method which looks like this:

m_Canvas.drawLine(rightSide, bottomSide, rightSide, topSide, boardPaint);
m_Canvas.drawLine(rightSide + 1, bottomSide, rightSide + 1, topSide, boardPaint);

HOW TO: Connect Windows Mobile Emulators

When you are writing a windows mobile application it is tough not to want to play with all the tools in the Windows Phone.  Being a personal fan of Android I find myself unfamiliar and want to know the environment I am supporting for clients (and I am a techie so it is easy to get lost in these things).  Recently I ran into an issue even accessing the internet on my mobile device and had to come up with steps to enable network access.  Here are some steps you can take to get mobile access on your device emulator:
1. In your Emulator window, navigate to File -> Configure
2. Enable the network adapter through the Network tab on the Emulator properties.
3. If you get an error message at this point about VPC drivers, follow this link to obtain them:  This is a free download around 30mb.
4. Start Windows Mobile Device Center from your Start Menu
5. Select Connection Settings and change the Allow connections option to DMA:
6. Start up your device emulator from Visual Studio. (Tools -> Device Emulator Manager)
7. Set your device into Cradled mode:
At this point you should be able to open up Internet Explorer and start browsing, or use any other feature that requires network activity.

Mobile development

Part of the problem with mobile development is deciding whether or not to actually write a mobile application.  This can be a big decision as mobile applications are usually native to a device such as android or ios.  At this point the only mobile devices that are. Net based are windows phones.  They take less than 10% of the market right now so the path I usually take is with website development.

Most phones are getting to a point where they support touch controls on websites etc.  A big advantage to gearing your site towards this is platform independence.  You can write a master page targeting different sized browsers such as tablet, phone and standard computer.  As always code reusability is the target.  Happy coding!