Graphics Flashcards
Define what a drawable is…
An XML file used to define visual elements such as graphics, icons and backgrounds.
Give some drawable types…
Bitmap, vector, shape layer.
Define what a canvas is…
An object that provides methods that enable drawing graphics onto a View and SurfaceView.
What are the 4 classes required to draw with a canvas?
View -> Displays a bitmap.
Bitmap -> Provide a surface to draw on.
Canvas -> API to draw on the Surface.
Paint -> Style what’s been drawn.
Define a SurfaceView…
A type of View that is dedicated to being drawn on, and operates off the UI thread.
When creating a SurfaceView, what must the class extend and impement?
Extends SurfaceView
Implements Runnable, SurfaceHolder.Callback
What are SurfaceViews and Generic Views used for?
Both are used for drawing graphics. Views are used for more simple graphics due to being on the UI thread, whereas SurfaceViews are used for more complex graphic implementations due to operating off the UI thread.
What are the differences between drawing on a SurfaceView and a Generic View? Give a pro and con of using each…
SurfaceView provides a separate Surface for drawing, and drawing operation is done on a worker thread. It’s more efficient due to performing drawing on a worker thread. However, is more complicated due to thread management.
Generic View is drawn on the UI thread. This has less overhead due to no creation and management of other threads. However, should only be used with simple graphics to to prevent overloading UI thread.
When should a SurfaceView be used?
When doing complex, computationally expensive graphics operations such as animations.
When should a Generic View be used?
When doing simple, unchanging graphics such as drawing images on buttons.
Give the 3 steps to drawing a Generic View?
1) Create a class that extends View. Override onDraw and onTouchEvent.
2) Add the custom view to the layout XML file.
3) When the View is inflated, the onCreate( ) method is called, and displays the generic View.
Define the 3 steps to drawing on a SurfaceView…
1) Define SurfaceView -> Create a class that extends SurfaceView and implements Runnable, SurfaceHolder.Callback. Call getHolder( ) to get a reference to the SurfaceHolder.
2) Setup and draw to the SurfaceView -> Define run( ) to create thread implementation, and start thread in surfaceCreated( ).
3) Handle User Interactions -> resume( ) and pause( ).
In which SurfaceView callback method is the drawing thread started?
surfaceCreated( ).
What does implementing a SurfaceHolder.Callback do?
Enables implementation of the callback methods.
What object does the onDraw( ) method provide?
A Canvas object which can then be used to perform drawing operations.