JVM/Android View knowledge points

JVM part of the basic memory

  • jdk8 canceled the permanent generation, jdk7 constant pool moved to the heap
  • In the new generation, the memory ratio of Eden and Survivor is 8:1:1. Only one survivor can be used at a time, and the actual availability is 90%. New age: The old age is 1:2.
  • Large objects directly enter the old age, and small objects enter Eden first. If they are not recycled when GC occurs, they enter the survivor and enter the old age after a certain number of times. Of course, it is not absolute. When the survivor memory is insufficient, it will enter the old age, and if the old age is insufficient, GC will be performed
  • The JVM object survival judgment adopts the reachability analysis method. GCRoot is usually the object of the permanent generation. When the object is unreachable, it will not be killed immediately. finalize() has the opportunity to "resurrect" the object, but it is best not to use this method.
  • Garbage collectors are basically based on mark-sweep, mark-sort and copy algorithms. When GC occurs, the user thread stops, the client mode garbage collector is serial and serial old (serial), and the server mode is PS scanvage (concurrent) and serial od. The collector of AndroidStudio itself uses CMS by default (concurrency, user threads will not stop shortening, based on copy and mark-sweep algorithm), and the collector of Eclipse itself uses G1 by default (the most advanced collector at present, using mark-sort and copy algorithm) , the stop the world time is further shortened)

View/ViewGroup part of the doubt solving record

  • VelocityTracker Swipe Velocity Tracking
  • TouchSlop minimum swipe distance (device dependent
  • GestureDetector (this goes without saying
  • Padding is invalid when customizing View
  • Once DOWN is not consumed in event distribution, subsequent events will not be distributed, and Activity.OnTouchEvent will be called directly. Once an event is intercepted, subsequent events directly call onTouchEven instead of onInteceptTouchEcent. Except DOWN, once other events are not consumed, Activity.onTouchEvent will be called directly.

The subject

About the relationship between measure(), onMeasure() and getWithMeasureDimenson, getWith() and so on. After viewing the source code, in fact, the parent ViewGroup calls measureChild(WithMargins) in onMeasure, and then calls getChildMeasureSpec(p1, p2, p3), p1 is the parent View MeasureSpec, p2 is padding(+margin+usedSpace), and p3 is layoutparameter.with , and then further call the child view measure(spec, spec) and then call onMeasure(spec, spec), onMeasure setMeasureDismenson does not include margin information! If the ViewGroup wants to support margin, it must define a MarginLayparameter of extends, and use measureChildWithMargin() to correctly obtain margin information other than padding. Layout() completes the View layout, and onLayout() completes the child View layout. Draw() first draws the background, calls onDraw() to draw itself, and finally draws the child View(dispaDraw()).

     protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

    这里的withUsed和heightUsed实在不是很懂

    protected void measureChildWithMargins(View child,
            int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                        + heightUsed, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }





    /**
     * Does the hard part of measureChildren: figuring out the MeasureSpec to
     * pass to a particular child. This method figures out the right MeasureSpec
     * for one dimension (height or width) of one child view.
     *
     * The goal is to combine information from our MeasureSpec with the
     * LayoutParams of the child to get the best possible results. For example,
     * if the this view knows its size (because its MeasureSpec has a mode of
     * EXACTLY), and the child has indicated in its LayoutParams that it wants
     * to be the same size as the parent, the parent should ask the child to
     * layout given an exact size.
     *
     * @param spec The requirements for this view
     * @param padding The padding of this view for the current dimension and
     *        margins, if applicable
     * @param childDimension How big the child wants to be in the current
     *        dimension
     * @return a MeasureSpec integer for the child
     * 可以看出这个方法判断ViewGroup除了padding,子View的    
     * margin,还有多少空间给子View内容(包括ziView的padding)
     */
    public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:
            if (childDimension >= 0) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:
            if (childDimension >= 0) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= 0) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325683176&siteId=291194637