Official custom control process
According to the official Android Developers website, custom controls require the following steps. (Some steps can be omitted according to business needs)
1. Create a View
2. Handle the layout of the View
3. Draw View
4. Interact with users
5. Optimize defined Views
The above five items are the custom control steps given by Android.
Each step includes many small knowledge points. You can remember these five points and understand the small knowledge points contained in each point. Add some practices of custom controls. Proficient in this knowledge, and believe that developers can define excellent custom controls. Next, we will elaborate on the five key points listed above.
Create View
1. Inherit View
Custom Views are inherited from Views. Of course, if the View you want to customize has some control functions already provided by Android, it can be directly inherited from the controls already provided.
2. Rewrite the construction method
- Xxx(Context context)
- Xxx(Context context, AttributeSet attrs)
- Xxx(Context context, AttributeSet attrs, int defStyleAttr)
3. Define custom properties
Custom properties are usually written in res/values/file. Declaring custom attributes all belong to styleable. Generally, the name of the styleable is the same as the class name of the custom control.
4. Get custom properties
- When a view is created in xml, all attributes declared in xml will be passed into the AttributeSet type parameters in the view constructor.
- Return a TypedArray object by calling the obtainedStyledAttributes() method of the Context. Then use the TypedArray object to get the value of the custom attribute.
- The TypedArray object is a shared resource, so after obtaining the value, you must call the recycle() method to recycle.
Measure View(Measure)
1. Measurement
A View has width and height when displaying. Measurement of View is to enable custom controls to display with appropriate width and height according to various situations. The measurement must be mentioned onMeasure method. The onMeasure method is a view to determine the width and height.
2. Rewrite the fixed pseudo-code writing method of onMeasure:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measureWidth = measure(widthMeasureSpec, true); int measureHeight = measure(heightMeasureSpec, false); setMeasuredDimension(measureWidth, measureHeight); }
After calculating height and width, the setMeasuredDimension() method should be called in onMeasure. Otherwise, a runtime exception will occur.
3. Calculate the value required by some custom controls onSizeChange()
The onSizeChange() method will be called when the view is specified for the first time, or the view's size changes.
Draw View(Draw)
1. After the custom control is created and the measurement code is written, then call onDraw() to draw the View
- The onDraw method contains a parameter called canvas. OnDraw() simply put two points: Canvas decides what to draw; Paint decides how to draw
- Canvas provides a method of drawing lines, and Paint determines the color of the lines. Canvas provides drawing rectangles, and Paint can decide whether to make the rectangle hollow or solid.
2. Before starting drawing in onDraw method
The brush Paint object information must be initialized. Because View re-draw is relatively frequent and onDraw may be called multiple times, the initialized code should not be placed in the onDraw method.
brush
It plays an extremely important role in the drawing process. The brush mainly saves color, style and other drawing information, specifying how to draw text and graphics. There are many settings methods for brush objects, which can be roughly divided into two categories, one is related to graphic drawing and the other is related to text drawing.
canvas
After adjusting the brush, you need to draw it on the canvas, which requires the Canvas class. Canvas can draw anything. You also need to set some properties about the canvas, such as the color and size of the canvas.
5. What are the common drawing operations
- drawRect, drawRoundRect, drawRoundRect, draw rectangle
- drawOval, drawCircle, drawArc: draw ellipse, circle, and arc
- drawText: Draw text
- drawBitmap: Draw pictures
Introduction and difference between RectF
Rect's parameter is of int type, while RectF's parameter type is of float type. From this point of view, RectF has higher accuracy, but they all use four coordinate parameters to determine the area of a rectangle.
Interact with users
- In some cases, custom controls not only show beautiful styles, but also need to support user clicks and drag operations. Custom controls require user interaction.
- The most common event in the Android system is the touch event, which will call the View's onTouchEvent(). Rewrite this method to handle the event logic.
- Touch has more gestures, such as tapping, swiping, etc., so when supporting special user interaction, you need to use the GestureDetector provided by Android. You only need to implement the corresponding interface in the GestureDetector and process the corresponding callback method.
- In addition to gestures, if there are movements or other situations, the sliding animation needs to be displayed smoothly. The animation should be a smooth start and end, rather than a sudden disappearance and a sudden start. It is recommended to use attribute animation
Optimize custom view
After the above steps are over, a relatively complete custom control has actually come out. Next, you need to make sure that the custom controls run smoothly. The official statement is: to avoid slow control experience, make sure that the animation maintains the effect of 60 frames per second as much as possible.
Optimization suggestions given by the official website:
- 1. Avoid unnecessary code
- 2. There should be no code in the onDraw() method that will cause garbage collection.
- 3. Make the onDraw() method call as little as possible. Most onDraw() method calls are accompanied by calling invalidate(), so it is not necessary, so do not call the invalidate() method.
This is the end of this article about the detailed explanation of the process of implementing custom View controls on Android. For more related content on Android custom View controls, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!