Ali interviewer; Android Custom View You know it?

 

 

Foreword

View, there are a lot of names. Whether you are familiar with the layout, or controls, all of them are inherited from View.

 

Article reprinted from Carson_Ho part of the picture Gangster article

mind Mapping

 

work process

measure

In fact, by the layout of the second map that we know the size of the calculated control.

  • height = bottom - top
  • width = right - left

For ViewGroup concerned, it is to traverse a container of child controls and calculated.

Because direct inherited from the View controls use wrap_cotent and match_parent is shown the effect is the same. We need to use getMode () method in MeasureSpec to distinguish and compare the current mode.

UNSPECIFIED mode state is not specified pattern, much like View on how big the parent container is not limited, and is generally used to measure the maximum pattern AT_MOST inside the system, corresponding to the wrap_content, SpecSize value is not greater than the size of the View EXACTLY fine mode, corresponding to match_parent, View, the size of the value SpecSize

 
@Override 

layout

In determining the location, we have a great need for major place - coordinate system.

Android coordinate system and the coordinate system usually is not the same drawing.

 

Therefore, corresponding to our natural position calculation method and we had just the opposite.

 

4 are determined by the position of the vertex of four values:

  • Top : child View on the distance to the border where border container.
  • left : child View from the left edge of the container where the left boundary.
  • bottom : Sub View distance to the boundary where the boundary of the container.
  • right : child View right border to a distance where the left edge of the container.

All calculations are relative to where the vessel will be able to start.

draw

A total of six steps:

  1. If necessary, draw the background - drawBackground (Canvas);
  2. Save the current canvas layer - SAVECOUNT canvas.getSaveCount = ();
  3. View the contents of the drawing - IF onDraw (Canvas); (dirtyOpaque!)
  4. Drawing Sub View - dispatchDraw (Canvas);
  5. If desired, the edge is drawn faded View, similar to the shadow effect - canvas.restoreToCount (SAVECOUNT);
  6. Draw decorations, such as scroll bars - onDrawForeground (Canvas);

Relates to a method developers need to rewrite the contents of the third step is generally drawn View corresponding onDraw ().

 
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 

Getting Custom View

In everyday project layout file we often use the xmlns: app = "http://schemas.android.com/apk/res-auto" this label, in fact, he is used to introduce our custom label use.

  1. Attrs created under res / values ​​directory
 
<?xml version="1.0" encoding="utf-8"?> 
  1. Acquired in DefaultView (Context context, @Nullable AttributeSet attrs). The following is the entire complete code.
 
/** 

The basis of performance optimization

First, if we first understand how to know whether a View is over drawn?

In fact, our phone development model has been the existence of this option a.

Before turning to open later

 

 

Drawn below the given number of times corresponding to FIG.

 

Then how do performance optimization it?

Before this problem, you need to understand what is overdraw, you can be understood as useless data control in the same position of constantly superimposed produced, then we are concerned that it concentrated solutions.

Scheme 1: reduce nesting.

Using a linear layout | using constraints layout |

 

 

Because it is only a case, meaning to say, if a plurality of nested LinearLayout effect achieved, if a ConstraintLayout be direct, then use the latter alternative, since this is not repeated in the same area.

Scenario 2: remove the default background

 

This solution will actually issue is automatically drawn against the background, if we eliminate this level, from the perspective of the old saying is also a draw promoted. As shown generally directly reduce drawing layer.

Embodied in the code, by **** style.xml modify the Theme:

 
<item name="android:windowBackground">@null</item> 

to sum up

 

 

These are the results of my study, if there is anything I do not think the memory of the place or at the wrong article, please share with me.

Published 56 original articles · won praise 1 · views 2904

Guess you like

Origin blog.csdn.net/chuhe1989/article/details/104679918