Android loading Background details in View

  • 2020-06-12 10:41:15
  • OfStack

The most common task for most Android developers is to layout the interface, and loading background images is the most common task in View. But we have paid little attention to this process, and this article focuses on the process of loading background images in view. Understanding the loading of background images (resource loading) in view allows us to optimize the process of resource loading and make it easier when the entire application needs to be skinned.

The most common way to load View images is by setting drawable in the XML file and letting the Android system do it for us, or by manually writing code to load Bitmap and then loading it onto View. This article mainly analyzes when and how Android helped us to load the background image, so we can choose from Activity. setContentView or LayoutInflater. inflate(...). Methods start to analyze.

Whether from Activity.setContentView(...) Or LayoutInflater. inflate (...). Method initializes View and ends up in LayoutInflater.inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot). Here we focus on the loading of View's background images, leaving out how XML is parsed and loaded.


    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context)mConstructorArgs[0];
            mConstructorArgs[0] = mContext;
            View result = root;
            try {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }
                final String name = parser.getName();
                if (DEBUG) {
                    System.out.println("**************************");
                    System.out.println("Creating root view: "
                            + name);
                    System.out.println("**************************");
                }
                if (TAG_MERGE.equals(name)) {
                    if (root == null || !attachToRoot) {
                        throw new InflateException("<merge /> can be used only with a valid "
                                + "ViewGroup root and attachToRoot=true");
                    }
                    rInflate(parser, root, attrs, false);
                } else {
                    // Temp is the root view that was found in the xml
                    View temp;
                    if (TAG_1995.equals(name)) {
                        temp = new BlinkLayout(mContext, attrs);
                    } else {
                        temp = createViewFromTag(root, name, attrs);
                    }
                    ViewGroup.LayoutParams params = null;
                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }
                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }
                     // Inflate all children under temp
                    rInflate(parser, temp, attrs, true);
                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }
                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }
                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }
            } catch (XmlPullParserException e) {
                InflateException ex = new InflateException(e.getMessage());
                ex.initCause(e);
                throw ex;
            } catch (IOException e) {
                InflateException ex = new InflateException(
                        parser.getPositionDescription()
                        + ": " + e.getMessage());
                ex.initCause(e);
                throw ex;
            } finally {
                // Don't retain static reference on context.
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;
            }
            return result;
        }
    }
It's this long up here 1 String code, in fact, very clear thinking, is aimed at XML The file is parsed and then based on XML Each of these 1 Node to node View Initialize, which will follow View the Layout Parameters set to View Go, and then go View Add to its parent control.
In order to understand View How is it loaded, we just need to know
 temp = createViewFromTag(root, name, attrs);
Follow along.
    /*
     * default visibility so the BridgeInflater can override it.
     */
    View createViewFromTag(View parent, String name, AttributeSet attrs) {
        if (name.equals("view")) {
            name = attrs.getAttributeValue(null, "class");
        }
        if (DEBUG) System.out.println("******** Creating view: " + name);
        try {
            View view;
            if (mFactory2 != null) view = mFactory2.onCreateView(parent, name, mContext, attrs);
            else if (mFactory != null) view = mFactory.onCreateView(name, mContext, attrs);
            else view = null;
            if (view == null && mPrivateFactory != null) {
                view = mPrivateFactory.onCreateView(parent, name, mContext, attrs);
            }
            if (view == null) {
                if (-1 == name.indexOf('.')) {
                    view = onCreateView(parent, name, attrs);
                } else {
                    view = createView(name, null, attrs);
                }
            }
            if (DEBUG) System.out.println("Created view is: " + view);
            return view;
        } catch (InflateException e) {
            throw e;
        } catch (ClassNotFoundException e) {
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Error inflating class " + name);
            ie.initCause(e);
            throw ie;
        } catch (Exception e) {
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Error inflating class " + name);
            ie.initCause(e);
            throw ie;
        }
    }

The point of the above code is that try... Catch. What is wrapped in try is the initialization of View. Notice that there are several Factory in the above code that can be initialized in View, which means that we can actually interfere with the initialization of View here. As we can see from the above code, if we have a custom Factory, the current View to be initialized will take precedence over our custom Factory instead of the default Factory. So if we want to customize Factory, where should we define it? It's easy to imagine that Factory has to be customized before the resource loads, so we should start with onCreate(...). The this. setContentView (...). Previously set LayoutInflater.Factory.

getLayoutInflater().setFactory(factory);
So let's look at the function up here


  if (-1 == name.indexOf('.')) {
        view = onCreateView(parent, name, attrs);
    } else {
        view = createView(name, null, attrs);
    }

This function is to initialize View, there are two cases, one is the system comes with View, it in

if (-1 == name.indexOf('.'))
This is initialized because if the system comes with View, the one passed in usually does not have the system prefix "android.view." The other branch initializes is our custom View. We follow up with onCreateView to see.


  protected View onCreateView(String name, AttributeSet attrs)
            throws ClassNotFoundException {
        return createView(name, "android.view.", attrs);
    }
    public final View createView(String name, String prefix, AttributeSet attrs)
            throws ClassNotFoundException, InflateException {
        Constructor<? extends View> constructor = sConstructorMap.get(name);
        Class<? extends View> clazz = null;
        try {
            if (constructor == null) {
                // Class not found in the cache, see if it's real, and try to add it
                clazz = mContext.getClassLoader().loadClass(
                        prefix != null ? (prefix + name) : name).asSubclass(View.class);
                if (mFilter != null && clazz != null) {
                    boolean allowed = mFilter.onLoadClass(clazz);
                    if (!allowed) {
                        failNotAllowed(name, prefix, attrs);
                    }
                }
                constructor = clazz.getConstructor(mConstructorSignature);
                sConstructorMap.put(name, constructor);
            } else {
                // If we have a filter, apply it to cached constructor
                if (mFilter != null) {
                    // Have we seen this name before?
                    Boolean allowedState = mFilterMap.get(name);
                    if (allowedState == null) {
                        // New class -- remember whether it is allowed
                        clazz = mContext.getClassLoader().loadClass(
                                prefix != null ? (prefix + name) : name).asSubclass(View.class);
                        boolean allowed = clazz != null && mFilter.onLoadClass(clazz);
                        mFilterMap.put(name, allowed);
                        if (!allowed) {
                            failNotAllowed(name, prefix, attrs);
                        }
                    } else if (allowedState.equals(Boolean.FALSE)) {
                        failNotAllowed(name, prefix, attrs);
                    }
                }
            }
            Object[] args = mConstructorArgs;
            args[1] = attrs;
            final View view = constructor.newInstance(args);
            if (view instanceof ViewStub) {
                // always use ourselves when inflating ViewStub later
                final ViewStub viewStub = (ViewStub) view;
                viewStub.setLayoutInflater(this);
            }
            return view;
        } catch (NoSuchMethodException e) {
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Error inflating class "
                    + (prefix != null ? (prefix + name) : name));
            ie.initCause(e);
            throw ie;
        } catch (ClassCastException e) {
            // If loaded class is not a View subclass
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Class is not a View "
                    + (prefix != null ? (prefix + name) : name));
            ie.initCause(e);
            throw ie;
        } catch (ClassNotFoundException e) {
            // If loadClass fails, we should propagate the exception.
            throw e;
        } catch (Exception e) {
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Error inflating class "
                    + (clazz == null ? "<unknown>" : clazz.getName()));
            ie.initCause(e);
            throw ie;
        }
    }

From onCreateView (...). As we know, createViewFromTag(...) The final initialization of View is through createView(...) This function is initialized, except that the system controls need to pass through onCreateView(...) Prefix it so that the class loader (ClassLoader) properly initializes the class through its package. createView (...). The first two branches of try are used to extract class constructors to be used. The Android system caches used class constructors because commonly used controls like TextView may be used many times. The next step is to initialize View through the class constructor. Notice that mConstructorArgs passed into the constructor is an array of two elements.

final Object[] mConstructorArgs = new Object[2];
It is then clear that it is the constructor that calls the corresponding two parameters in the system control. For convenience, we will analyze from the most basic View.


  public View(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public View(Context context, AttributeSet attrs, int defStyle) {
     this(context);
     TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
             defStyle, 0);
     Drawable background = null;
     int leftPadding = -1;
     int topPadding = -1;
     int rightPadding = -1;
     int bottomPadding = -1;
     int startPadding = UNDEFINED_PADDING;
     int endPadding = UNDEFINED_PADDING;
     int padding = -1;
     int viewFlagValues = 0;
     int viewFlagMasks = 0;
     boolean setScrollContainer = false;
     int x = 0;
     int y = 0;
     float tx = 0;
     float ty = 0;
     float rotation = 0;
     float rotationX = 0;
     float rotationY = 0;
     float sx = 1f;
     float sy = 1f;
     boolean transformSet = false;
     int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
     int overScrollMode = mOverScrollMode;
     boolean initializeScrollbars = false;
     boolean leftPaddingDefined = false;
     boolean rightPaddingDefined = false;
     boolean startPaddingDefined = false;
     boolean endPaddingDefined = false;
     final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
     final int N = a.getIndexCount();
      for (int i = 0; i < N; i++) {
          int attr = a.getIndex(i);
          switch (attr) {
              case com.android.internal.R.styleable.View_background:
                  background = a.getDrawable(attr);
                  break;
              case com.android.internal.R.styleable.View_padding:
                  padding = a.getDimensionPixelSize(attr, -1);
                  mUserPaddingLeftInitial = padding;
                  mUserPaddingRightInitial = padding;
                  leftPaddingDefined = true;
                  rightPaddingDefined = true;
                  break;
   // omit 1 A bunch of independent functions
 }

Since we only care about how the background image in View is loaded, notice that this function is really just walking through AttributeSet attrs and then initializing the properties of View. We go straight in

background = a.getDrawable(attr);
See here (ES115en.getDrawable).


    public Drawable getDrawable(int index) {
        final TypedValue value = mValue;
        if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) {
            if (false) {
                System.out.println("******************************************************************");
                System.out.println("Got drawable resource: type="
                                   + value.type
                                   + " str=" + value.string
                                   + " int=0x" + Integer.toHexString(value.data)
                                   + " cookie=" + value.assetCookie);
                System.out.println("******************************************************************");
            }
            return mResources.loadDrawable(value, value.resourceId);
        }
        return null;
    }

We find that it calls mResources. loadDrawable(...) Go in and have a look.


    /*package*/ Drawable loadDrawable(TypedValue value, int id)
            throws NotFoundException {
        if (TRACE_FOR_PRELOAD) {
            // Log only framework resources
            if ((id >>> 24) == 0x1) {
                final String name = getResourceName(id);
                if (name != null) android.util.Log.d("PreloadDrawable", name);
            }
        }
        boolean isColorDrawable = false;
        if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
                value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
            isColorDrawable = true;
        }
        final long key = isColorDrawable ? value.data :
                (((long) value.assetCookie) << 32) | value.data;
        Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);
        if (dr != null) {
            return dr;
        }
        Drawable.ConstantState cs = isColorDrawable
                ? sPreloadedColorDrawables.get(key)
                : (sPreloadedDensity == mConfiguration.densityDpi
                        ? sPreloadedDrawables.get(key) : null);
        if (cs != null) {
            dr = cs.newDrawable(this);
        } else {
            if (isColorDrawable) {
                dr = new ColorDrawable(value.data);
            }
            if (dr == null) {
                if (value.string == null) {
                    throw new NotFoundException(
                            "Resource is not a Drawable (color or path): " + value);
                }
                String file = value.string.toString();
                if (TRACE_FOR_MISS_PRELOAD) {
                    // Log only framework resources
                    if ((id >>> 24) == 0x1) {
                        final String name = getResourceName(id);
                        if (name != null) android.util.Log.d(TAG, "Loading framework drawable #"
                                + Integer.toHexString(id) + ": " + name
                                + " at " + file);
                    }
                }
                if (DEBUG_LOAD) Log.v(TAG, "Loading drawable for cookie "
                        + value.assetCookie + ": " + file);
                if (file.endsWith(".xml")) {
                    try {
                        XmlResourceParser rp = loadXmlResourceParser(
                                file, id, value.assetCookie, "drawable");
                        dr = Drawable.createFromXml(this, rp);
                        rp.close();
                    } catch (Exception e) {
                        NotFoundException rnf = new NotFoundException(
                            "File " + file + " from drawable resource ID #0x"
                            + Integer.toHexString(id));
                        rnf.initCause(e);
                        throw rnf;
                    }
                } else {
                    try {
                        InputStream is = mAssets.openNonAsset(
                                value.assetCookie, file, AssetManager.ACCESS_STREAMING);
        //                System.out.println("Opened file " + file + ": " + is);
                        dr = Drawable.createFromResourceStream(this, value, is,
                                file, null);
                        is.close();
        //                System.out.println("Created stream: " + dr);
                    } catch (Exception e) {
                        NotFoundException rnf = new NotFoundException(
                            "File " + file + " from drawable resource ID #0x"
                            + Integer.toHexString(id));
                        rnf.initCause(e);
                        throw rnf;
                    }
                }
            }
        }
        if (dr != null) {
            dr.setChangingConfigurations(value.changingConfigurations);
            cs = dr.getConstantState();
            if (cs != null) {
                if (mPreloading) {
                    if (verifyPreloadConfig(value, "drawable")) {
                        if (isColorDrawable) {
                            sPreloadedColorDrawables.put(key, cs);
                        } else {
                            sPreloadedDrawables.put(key, cs);
                        }
                    }
                } else {
                    synchronized (mTmpValue) {
                        //Log.i(TAG, "Saving cached drawable @ #" +
                        //        Integer.toHexString(key.intValue())
                        //        + " in " + this + ": " + cs);
                        if (isColorDrawable) {
                            mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                        } else {
                            mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                        }
                    }
                }
            }
        }
        return dr;
    }

This is the function where all the View background is loaded. The logic for this function is a bit more complicated, and basically, depending on the type of background (pure color, defined in an XML file, or a static background), if there is one in the cache, use it.

In conclusion 1, after the above analysis, we know that Android is in ES134en. setContentView(...). To be precise, the resource file is loaded in the constructor of each initialized View.

The above is the entire content of this article, I hope to be helpful to you.


Related articles: