Unit 2 - Practice Quiz
1
What is the primary purpose of the Canvas class in Android?
2
Which class is used to specify the color, style, and font for drawing on a Canvas?
Brush
Style
Color
Paint
3
Which Canvas method would you use to draw a circle?
drawOval()
drawCircle()
drawPoint()
drawArc()
4
What is the purpose of the onDraw(Canvas canvas) method in a custom View?
5
If you want to draw only the outline of a rectangle, which Paint.Style should you use?
Paint.Style.STROKE
Paint.Style.FILL_AND_STROKE
Paint.Style.OUTLINE
Paint.Style.FILL
6
Which method is used to draw text on a Canvas?
writeText()
printString()
drawText()
drawLabel()
7
To draw a straight line between two points on a Canvas, which method is used?
connectPoints()
drawPath()
drawLine()
drawVector()
8 What are the two main animation systems available in Android?
9 Which type of animation actually modifies the properties of the target object, allowing it to respond to clicks at its new location?
10 In which resource directory are XML files for View Animations (tween animations) typically stored?
res/drawable/
res/animator/
res/anim/
res/layout/
11
What is the role of an Interpolator in Android animations?
12
Which class is the simplest way to animate a View's property, such as alpha or rotation, by name?
ValueAnimator
ObjectAnimator
AnimatorSet
ViewPropertyAnimator
13
What is the primary function of the ValueAnimator class?
14
What does the <alpha> XML tag represent in a View Animation file?
15 What is the main purpose of the Android Transition framework?
16
In the Transition framework, what does a Scene represent?
17
Which predefined Transition is specifically designed to animate the appearance and disappearance of views?
Move
Fade
ChangeBounds
ChangeImageTransform
18
Which method is called to begin the animation from the current layout to a new Scene?
Animator.start(scene)
Scene.enter()
View.transitionTo(scene)
TransitionManager.go(scene)
19 What is a "Shared Element Transition" primarily used for?
20 Which transition class automatically animates changes to a view's position and size?
ChangeBounds
Slide
Explode
Fade
21
In a custom View, you want to draw a circle, rotate the coordinate system by 45 degrees, and then draw a square centered at the same point as the circle. What will be the final visual outcome?
22
You are implementing a custom drawing View. To apply a complex transformation (e.g., a rotation followed by a translation) and then revert to the original state for subsequent drawing operations, which pair of Canvas methods is most efficient?
Canvas object for the transformed drawing.
23
What is the primary difference between a ValueAnimator and an ObjectAnimator?
ObjectAnimator can be used in an AnimatorSet, but ValueAnimator cannot.
ValueAnimator can only animate integer values, while ObjectAnimator can animate any property.
ValueAnimator is part of the older View Animation system, while ObjectAnimator is part of the Property Animation system.
ObjectAnimator directly modifies a target object's property, while ValueAnimator only calculates animated values and requires a listener to apply them.
24
To create a shared element transition between two Activities, ActivityA and ActivityB, which XML attribute is essential for linking the shared views?
25
Consider the following code in onDraw(Canvas canvas):
java
paint.setColor(Color.BLUE);
canvas.translate(100, 100);
canvas.drawRect(0, 0, 50, 50, paint);
paint.setColor(Color.RED);
canvas.scale(2, 2, 0, 0); // Scale around the current origin's (0,0) point
canvas.drawRect(0, 0, 25, 25, paint);
What is the final appearance on the screen?
26
You want to animate a custom view's property named cornerRadius using ObjectAnimator. The custom view class does not have a setCornerRadius(float radius) method. What will happen when you try to run the animation?
ObjectAnimator will automatically create the setter method using reflection.
NoSuchMethodException.
27
What is the primary purpose of the TransitionManager.beginDelayedTransition(ViewGroup root) method?
ViewGroup before and after the next layout pass, and automatically animate the changes.
28
You use a LinearInterpolator for an animation. How does this affect the animation's rate of change over time?
29
What is the main performance-related reason to avoid object allocations inside the onDraw() method of a custom View?
onDraw() can cause memory leaks.
onDraw() is called frequently, and repeated allocations can trigger the Garbage Collector, causing stuttering or 'jank'.
onDraw() consumes excessive battery power.
Paint and Path objects cannot be instantiated inside onDraw().
30
When creating a custom Transition, which two methods are the most critical to override to define the transition's behavior?
onAppear() and onDisappear()
setDuration() and setInterpolator()
start() and end()
captureStartValues(TransitionValues) and captureEndValues(TransitionValues)
31
How does a Property Animation (e.g., ObjectAnimator) differ from a View Animation in terms of its effect on the animated View?
View subclasses.
View object, while View Animations only change how the View is drawn on the screen.
32
You need to draw a shape that is the intersection of a circle and a rectangle. Which Canvas method would be most suitable for this task?
canvas.drawPath() with two separate paths.
PorterDuff.Mode.SRC_IN Xfermode on a Paint object.
canvas.drawVertices() with a custom vertex buffer.
canvas.clipPath(circlePath) followed by canvas.drawRect(...).
33
You want to run three animations simultaneously: fade in a TextView, scale up an ImageView, and move a Button. Which component is best suited for coordinating these animations?
AnimationSet from the View Animation framework.
AnimatorSet with playTogether().
Thread that updates view properties manually.
ValueAnimator with multiple update listeners.
34
In the Material Design transition framework, what is the purpose of the Explode transition?
Path.
35
To draw a quadratic Bézier curve on a Canvas, your Path object needs a starting point, an end point, and one other point. What is the role of this third point?
36
You want to animate a background color change from red to blue smoothly. Which class would you typically use with a ValueAnimator or ObjectAnimator to ensure correct color interpolation?
37
You are using TransitionManager.beginDelayedTransition() to animate a layout change, but you want one specific ImageView to remain static. How can you achieve this?
transition.removeTarget(imageView) method.
ViewGroup is always animated.
ImageView's android:animateLayoutChanges attribute to false.
ImageView's visibility to GONE before the transition starts.
38
What is the purpose of a Keyframe in a property animation?
39
You are implementing a Fragment transaction and want the exiting fragment to slide to the left and the entering fragment to slide in from the right. Which method would you use on the FragmentTransaction to achieve this?
addSharedElement()
setTransitionStyle()
setSharedElementEnterTransition()
setCustomAnimations()
40
You are drawing text on a Canvas and want it to be perfectly centered horizontally at a specific x-coordinate. Which Paint setting is crucial for this?
paint.setTextSize(50f)
paint.setStyle(Paint.Style.FILL_AND_STROKE)
paint.setTextAlign(Paint.Align.CENTER)
paint.setLetterSpacing(0.1f)
41
You are implementing a custom View where you need to draw a shape and then apply a PorterDuff.Mode.XOR blend mode to a semi-transparent overlay. To ensure the blend mode only affects the specific shape and not the entire View background, which Canvas operation is most appropriate and why?
Bitmap and Canvas, drawing there, and then using canvas.drawBitmap(): This works but is less efficient than saveLayer for in-place, isolated compositing.
canvas.save() followed by setting an Xfermode on the Paint: This will apply the blend mode to everything drawn with that paint, potentially affecting the View's background if not clipped correctly.
canvas.saveLayer(bounds, paint): It creates an off-screen buffer, allowing blend modes to operate in isolation on subsequent drawing commands before being composited back.
canvas.save() followed by canvas.clipRect(bounds): Clipping restricts the drawing area but does not isolate the blending operation from the existing canvas content.
42
Consider the following Matrix operations applied sequentially: matrix.setScale(2, 2); matrix.postTranslate(50, 0); matrix.preRotate(90);. If this matrix is applied to a point at (10, 20), what will be the final transformed coordinates? Remember the transformation formula is .
43
You are creating a custom Interpolator to model a bounce effect. The desired behavior is that the animation overshoots to 1.2, then settles back to 1.0. Which getInterpolation(float input) implementation would best produce this effect near the end of the animation?
return (float)(-1 * Math.pow(Math.E, -input / 0.3) * Math.cos(10 * input) + 1); which models a dampened harmonic oscillator.
(float)(-0.5 * Math.cos(input * Math.PI) + 0.5) which only produces values between 0 and 1.
float t = input; t *= t; return t * t * ((2.75f * t) - 1.75f); which is a standard ease-in interpolator.
new OvershootInterpolator(2.0f) which provides a simple overshoot but without the 'bounce back' behavior.
44
You are implementing a custom Transition to animate a TextView's color from a start color to an end color. In which lifecycle methods of the Transition class must you capture the color values, and which method is responsible for creating the Animator?
createAnimator(), end color in onTransitionEnd(). Create the Animator in captureStartValues().
createAnimator(). The Animator is also created there.
captureStartValues(), capture the end color in captureEndValues(). Create the Animator (e.g., a ValueAnimator) in createAnimator().
captureStartValues(), end color in onTransitionPause(). Create the Animator in captureEndValues().
45
An AnimatorSet is configured as follows: Animator a, b, c, d; ... AnimatorSet set = new AnimatorSet(); set.play(a).with(b); set.play(c).after(a); set.play(d).after(b); What is the execution order of these animators?
46
When drawing a complex Path with many curves and lines on a hardware-accelerated Canvas, which of the following operations is most likely to cause a significant performance drop or force a fallback to software rendering?
Matrix to the Canvas before drawing the path.
Paint object.
Paint object that has a ColorFilter applied.
Path.op(path1, path2, Path.Op.DIFFERENCE) to create a complex shape from two other paths before drawing.
47
In a shared element transition between a RecyclerView (Fragment A) and a detail view (Fragment B), you notice flickering or incorrect positioning. The item in the RecyclerView is temporarily replaced with the detail view before the transition ends. What is a likely cause and solution?
Window content transitions are disabled. The solution is to enable them in the theme with <item name="android:windowActivityTransitions">true</item>.
transitionName to each view holder's view.
postponeEnterTransition() was not called in Fragment B's onViewCreated. The solution is to call it and then call startPostponedEnterTransition() once the shared element is measured and laid out.
ImageView's scaleType is different in the two fragments, causing a measurement mismatch. The solution is to use android:scaleType="centerCrop" on both.
48
You need to animate a custom object property, Circle.centerPoint, which is a PointF. The property does not have a public setter setCenterPoint(PointF). Which combination of animator and helper class is the most efficient and avoids reflection?
ObjectAnimator.ofObject(myCircle, "centerPoint", new PointFEvaluator(), newPoint).
Circle that has a setCenterPoint method and using ObjectAnimator on the wrapper.
ValueAnimator.ofObject(new PointFEvaluator(), startPoint, endPoint) and manually updating the property in an AnimatorUpdateListener.
Property object: ObjectAnimator.ofObject(myCircle, new Property<Circle, PointF>(PointF.class, "centerPoint") { ... }, new PointFEvaluator(), newPoint).
49
In a MotionLayout scene, you want a view to move along a circular arc path instead of a straight line between the start and end ConstraintSets. Which KeyFrameSet attribute is best suited for this?
KeyPosition with keyPositionType="parentRelative" and percentY="-0.5" at 50% of the animation.
KeyPosition with keyPositionType="pathRelative" and percentY="-0.5" at frame position 50.
KeyCycle with waveShape="sin" to create an oscillation.
KeyAttribute to change the view's rotation at the midpoint.
50
You use canvas.clipPath(myPath) to create a non-rectangular clipping region. Afterwards, you apply a rotation to the canvas with canvas.rotate(45). What is the result of subsequent drawing operations?
IllegalStateException as transformations are not allowed after clipping to a complex path.
clipPath operation is ignored because it was followed by a transformation.
51
You are using a SpringAnimation to create a physics-based movement. You want the animation to be very bouncy and overshoot its final position significantly, oscillating multiple times before settling. Which combination of stiffness and dampingRatio should you use?
stiffness = SpringForce.STIFFNESS_HIGH, dampingRatio = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
stiffness = SpringForce.STIFFNESS_VERY_LOW, dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY
stiffness = SpringForce.STIFFNESS_MEDIUM, dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY
stiffness = SpringForce.STIFFNESS_LOW, dampingRatio = SpringForce.DAMPING_RATIO_NO_BOUNCY
52
What is the primary purpose of the TransitionValues object in the Android Transition Framework, and what is a common mistake when using its values Map?
View itself; a mistake is holding onto this reference, causing memory leaks.
Animator for a view; a mistake is trying to store non-serializable objects.
String key like "color" which can cause collisions with other transitions.
53
You're creating a ComposeShader to combine a BitmapShader (a texture) and a LinearGradient shader. If you use new ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.MULTIPLY), what will the visual outcome be?
LinearGradient will be visible, as the second shader in the constructor takes precedence.
54
When using ObjectAnimator to animate a custom view property, you notice jank and skipped frames. The animation is simple, animating a single float property. Profiling shows significant time spent in Method.invoke(). What is the most effective way to optimize this animation without rewriting it to use ValueAnimator?
ViewPropertyAnimator instead, via myView.animate()....
HandlerThread.
Property object for the animated property and passing it to ObjectAnimator.ofFloat() instead of the property name string.
55
You are using TransitionManager.beginDelayedTransition with a ChangeBounds transition to animate a View's change in layout parameters. However, you want the View's children to move with their parent but not resize during the transition. How can you achieve this?
setResizeClip(true) on the ChangeBounds instance.
TransitionListener and manually reset the children's scale to 1.0 on each frame.
ChangeBounds and requires no special configuration.
TransitionSet that excludes all children from the ChangeBounds transition using excludeTarget().
56
In your onDraw, you have a loop that draws 1,000 small, overlapping, semi-transparent circles. The performance is poor. Which change would likely provide the biggest performance improvement when hardware acceleration is enabled?
Paint object.
Bitmap with Canvas.saveLayer() and a new Paint, then drawing that bitmap once.
Paint object outside the loop.
Path using Path.addCircle() and drawing the combined path once.
57
You need to create a PathInterpolator that makes an animation start fast, slow down dramatically in the middle, and then speed up again at the end. How would you define the control points (x1, y1) and (x2, y2) for its underlying cubic Bezier curve?
(0.8, 0.0) and (0.2, 1.0). The curve moves 'backwards' in time along the x-axis, creating an S-shape.
(0.5, 0.0) and (0.5, 1.0). This creates a linear interpolation.
(0.25, 0.1) and (0.25, 1.0). This is a standard ease-in-out curve.
(0.0, 0.8) and (1.0, 0.2). The curve is very steep at the start and end, and very flat in the middle.
58
You are using MotionLayout and want to trigger an animation based on a custom event, not just user swipes or layout changes. For example, you want to transition to the 'end' state when a network request completes. How would you programmatically trigger this transition?
motionLayout.transitionToState(R.id.end).
StateListAnimator defined in XML to link the network event to a state change.
Transition object in code and apply it using TransitionManager.go().
motionLayout.setTransition(R.id.start, R.id.end) followed by motionLayout.setProgress(1.0f).
59
You are drawing text on a Canvas. You need to ensure the exact vertical center of the text (from the top of the ascender to the bottom of the descender) is aligned with a specific y-coordinate, cy. Given a Paint object, how do you calculate the correct y value for the canvas.drawText call?
float y = cy - (paint.descent() + paint.ascent()) / 2;
Rect bounds = new Rect(); paint.getTextBounds("MyText", 0, 6, bounds); float y = cy - bounds.exactCenterY();
float y = cy - paint.getTextSize() / 2;
float y = cy;
60
You create a ValueAnimator that animates from 0 to 1 over 10 seconds. You set its repeat mode to REVERSE and repeat count to INFINITE. After 25 seconds, you call animator.getAnimatedValue(). Assuming a linear interpolator, what will the returned value be?
1.0
0.0
0.5
0.25