My Personal View

So today I wanted to share with everyone yet another set of tricks that I’ve learn from a previous JAVA class.  When I came over to mobile and learned that a big portion of its coding was also in JAVA, I started to experiment with my old skills.

So thinking back to inheritance of classes (in either c++ or JAVA), we know that the child class inherits the public and protected functions of the parent right? Now let’s take a very import class from the android API dissect it a bit. The View class is one of the most widely used classes in the API. It has a lot of cool features and function that are public and protected (and maybe a few private ones that we don’t know about). We also know that the View super class has children of its own like ScrollView and ListView. So the question is why can’t we extend View and make our own View. Well let the cloning began . . .

First, we should create a class that extends a view of your choice (ScrollView, View, ect.), then create the constructor that takes in a parameter with type context. Next, it the fun part. Every View has the onDraw function, that takes in a Canvas type parameter. Now the canvas that is passed in is basically the entire screen of your phone. The Canvas class also has countless amounts of functions that allow you to draw, pictures, *bitmaps* (my favorite), shapes, text, and much, much more to the screen. The View class also has another function called “invalidate()”. By calling this function at the end of the onDraw function, it causes the onDraw function to redraw itself. Why is this function so special? Well let’s say we have a bitmap, that maps out the pixels of a figure stored in your resource folder and you’re currently writing a screensaver program to make an object continuously move across the screen. The invalidate() function along with some x and y value control will easily implement this feature. My group actually did something similar a few weeks ago. Check out the code.

A full example can be seen at this link: