A Surface is a drawing area that holds pixels to be displayed on the screen. It acts like a canvas where you can draw shapes, images, or text.
Click to reveal answer
beginner
How do you create a Shape drawable in Android?
You create a Shape drawable by defining an XML file in the res/drawable folder using the <shape> tag. You can specify properties like solid color, corners radius, and stroke width.
Click to reveal answer
intermediate
What is the difference between Canvas and SurfaceView?
Canvas is an object used to draw on a bitmap or surface. SurfaceView provides a dedicated drawing surface embedded inside a view hierarchy, allowing drawing on a separate thread for better performance.
Click to reveal answer
beginner
Why use ShapeDrawable instead of bitmap images for shapes?
ShapeDrawable is scalable and lightweight. It uses vector shapes defined in XML, so it looks sharp on all screen sizes and reduces app size compared to bitmap images.
Click to reveal answer
beginner
How can you make a rounded rectangle shape in Android XML?
Use the <shape> tag with shape="rectangle" and add <corners android:radius="10dp" /> inside it to round the corners.
Click to reveal answer
Which Android class provides a dedicated drawing surface for custom drawing on a separate thread?
AViewGroup
BCanvas
CSurfaceView
DDrawable
✗ Incorrect
SurfaceView provides a separate drawing surface that can be updated on a different thread for better performance.
What XML tag is used to define a shape drawable in Android?
A<bitmap>
B<shape>
C<vector>
D<drawable>
✗ Incorrect
The tag defines a shape drawable in XML.
Which property in a shape drawable XML controls the corner rounding?
Acorners
Bstroke
Cpadding
Dgradient
✗ Incorrect
The corners tag controls the radius of the shape's corners.
Why is ShapeDrawable preferred over bitmap images for simple shapes?
AIt can only be used in SurfaceView
BIt supports animations by default
CIt loads faster from the internet
DIt uses less memory and scales well
✗ Incorrect
ShapeDrawable is vector-based, so it uses less memory and scales without losing quality.
What does the Canvas class in Android do?
AProvides methods to draw shapes and text
BHandles user input events
CManages app navigation
DStores app data
✗ Incorrect
Canvas provides drawing methods to render shapes, text, and images.
Explain how SurfaceView and Canvas work together for custom drawing in Android.
Think about how you would draw moving shapes smoothly.
You got /4 concepts.
Describe how to create and use a shape drawable XML to make a button background with rounded corners.
Imagine styling a button with a colored background and smooth edges.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of SurfaceView in Android development?
easy
A. To provide a dedicated drawing surface for custom graphics
B. To handle user input events like clicks and touches
C. To manage app navigation between screens
D. To display static text content
Solution
Step 1: Understand SurfaceView role
SurfaceView provides a special area where you can draw graphics directly on a separate thread.
Step 2: Compare with other options
Handling input, navigation, or static text are not the main roles of SurfaceView.
Final Answer:
To provide a dedicated drawing surface for custom graphics -> Option A
Quick Check:
SurfaceView = Dedicated drawing surface [OK]
Hint: SurfaceView is for drawing graphics, not UI controls [OK]
Common Mistakes:
Confusing SurfaceView with View for UI controls
Thinking SurfaceView handles navigation
Assuming SurfaceView manages text display
2. Which of the following is the correct way to start drawing on a SurfaceView canvas in Kotlin?
easy
A. val canvas = surfaceView.getCanvas()
canvas.drawColor(Color.RED)
surfaceView.postCanvas(canvas)
B. val canvas = surfaceView.lockCanvas()
canvas.fill(Color.RED)
surfaceView.unlockCanvas(canvas)
C. val canvas = surfaceHolder.getCanvas()
canvas.clear(Color.RED)
surfaceHolder.postCanvas(canvas)
D. val canvas = surfaceHolder.lockCanvas()
canvas.drawColor(Color.RED)
surfaceHolder.unlockCanvasAndPost(canvas)
Solution
Step 1: Identify correct SurfaceHolder methods
To draw on SurfaceView, use surfaceHolder.lockCanvas() to get Canvas and unlockCanvasAndPost() to post.
Step 2: Check method names and usage
Only val canvas = surfaceHolder.lockCanvas()
canvas.drawColor(Color.RED)
surfaceHolder.unlockCanvasAndPost(canvas) uses correct methods: lockCanvas(), drawColor(), unlockCanvasAndPost().
Final Answer:
val canvas = surfaceHolder.lockCanvas()\ncanvas.drawColor(Color.RED)\nsurfaceHolder.unlockCanvasAndPost(canvas) -> Option D