Skip to content

Instantly share code, notes, and snippets.

@jwill
Last active August 29, 2015 14:02
Show Gist options
  • Save jwill/6e10a96fb5cfde9dd3a3 to your computer and use it in GitHub Desktop.
Save jwill/6e10a96fb5cfde9dd3a3 to your computer and use it in GitHub Desktop.
t
Knowing the standard widget library is nice, but as someone who was once accused as not so much recreating the wheel as hand machining my own nuts and bolts -- I know there are times when nothing in the toolchest will quite do the job. That’s when it’s time to dive in and build your own custom View from scratch.
We’re not including any custom Views in sunshine, but let’s take a look at how you *would* build one.
You start by creating a new Class for your view [draw]
If you’re building something from scratch rather than modifying an existing View, it should descend from either View itself [Draw]...or SurfaceView [Draw]
View offers a lightweight canvas-based approach, while SurfaceView is designed specifically to support UI elements that require rapid redraws and / or 3D graphics using something like openGL. It’s perfect for Views that display games or Videos.
The existing widget library Views are descended from View, so let’s take that approach [Circle View].
The base View class draws [Draw] an empty, borderless 100x100 pixel box.
To change that we override the onMeasure handler [draw] to indicate the View’s size,
and onDraw [draw] to draw its content.
If your View should always be an empty 100 pixel square, you’re in luck! Otherwise, we need to do some work. Let’s start by setting its size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment