Activity界面绘制过程详解

Activity启动过程详解 1. 简介 本文源码基于android 27。 startActivity的流程较复杂,我们这里将其过程分成三部分: startActivity–>ActivityManagerService ActivityManagerService–>ApplicationThread ApplicationThread–>Activity 这样看起来稍微简单点。 通 阅读详情

Activity界面绘制过程详解

设置界面首先就是Activity.setContentView()方法:我们先看一下他的源码:

/**
 * Set the activity content from a layout resource.  The resource will be
 * inflated, adding all top-level views to the activity.
 *
 * @param layoutResID Resource ID to be inflated.
 *
 * @see #setContentView(android.view.View)
 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
 */
public void setContentView(int layoutResID) {
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
}

getWindow()就是获取该页面的窗体Window,该类是一个抽象类,他有一个唯一的子类PhoneWindow.

/**
 * Retrieve the current {@link android.view.Window} for the activity.
 * This can be used to directly access parts of the Window API that
 * are not available through Activity/Screen.
 *
 * @return Window The current window, or null if the activity is not
 *         visual.
 */
public Window getWindow() {
    return mWindow;
}

接下来,我们看一下PhoneWindow.setContentView()方法。(简单的理解是PhoneWindow把DectorView(FrameLayout的子类)进行包装,将它作为窗口的根View`)

@Override
public void setContentView(int layoutResID) {

    // mContentParent 是当前window显示内容的父布局,它只能是mDecor或mDecor的子View,如果mContentParent为null,说明是第一次调用setContentView方法
    // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
    // decor, when theme attributes and the like are crystalized. Do not check the feature
    // before this happens.
    if (mContentParent == null) {
        // 内部会创建mContentParent, 如果mDecor为null就创建,然后如果mContentParent为null,就根据当前要显示的主题去添加对应的布局,
        // 并把该布局中id为content的ViewGroup赋值给mContentParent。
        installDecor();
    } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        // 多次调用setContentView,可能是第二次、第三次,先把之前的页面内容移除
        mContentParent.removeAllViews();
    }

    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                getContext());
        transitionTo(newScene);
    } else {
        // 把布局inflate后添加到mContentParent中
        mLayoutInflater.inflate(layoutResID, mContentParent);
    }
    final Callback cb = getCallback();
    if (cb != null && !isDestroyed()) {
        cb.onContentChanged();
    }
}

上面简单的说明了installDecor()的作用,这里我们在源码中仔细说明一下, 通过这个源码
我们知道设置主题要在setContentView之前去调用,如用代码设置requestWindowFeature()设置主题时要在setContentView()之前设置才有用.

private void installDecor() {
    if (mDecor == null) {
        // 内部就是new 一个 DecorView
        mDecor = generateDecor();
        mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        mDecor.setIsRootNamespace(true);
        if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
            mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
        }
    }
    if (mContentParent == null) {
        // 根据当前主题去选择对应的布局文件,然后把布局中的id=content(一般为FrameLayout)部分赋值给mContentParent.
        mContentParent = generateLayout(mDecor);

        // Set up decor part of UI to ignore fitsSystemWindows if appropriate.
        mDecor.makeOptionalFitsSystemWindows();

        final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(
                R.id.decor_content_parent);

        if (decorContentParent != null) {
            mDecorContentParent = decorContentParent;
            mDecorContentParent.setWindowCallback(getCallback());
            if (mDecorContentParent.getTitle() == null) {
                mDecorContentParent.setWindowTitle(mTitle);
            }

            final int localFeatures = getLocalFeatures();
            for (int i = 0; i < FEATURE_MAX; i++) {
                if ((localFeatures & (1 << i)) != 0) {
                    mDecorContentParent.initFeature(i);
                }
            }

            mDecorContentParent.setUiOptions(mUiOptions);

            if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) != 0 ||
                    (mIconRes != 0 && !mDecorContentParent.hasIcon())) {
                mDecorContentParent.setIcon(mIconRes);
            } else if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) == 0 &&
                    mIconRes == 0 && !mDecorContentParent.hasIcon()) {
                mDecorContentParent.setIcon(
                        getContext().getPackageManager().getDefaultActivityIcon());
                mResourcesSetFlags |= FLAG_RESOURCE_SET_ICON_FALLBACK;
            }
            if ((mResourcesSetFlags & FLAG_RESOURCE_SET_LOGO) != 0 ||
                    (mLogoRes != 0 && !mDecorContentParent.hasLogo())) {
                mDecorContentParent.setLogo(mLogoRes);
            }

            // Invalidate if the panel menu hasn't been created before this.
            // Panel menu invalidation is deferred avoiding application onCreateOptionsMenu
            // being called in the middle of onCreate or similar.
            // A pending invalidation will typically be resolved before the posted message
            // would run normally in order to satisfy instance state restoration.
            PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
            if (!isDestroyed() && (st == null || st.menu == null)) {
                invalidatePanelMenu(FEATURE_ACTION_BAR);
            }
        } else {
            mTitleView = (TextView)findViewById(R.id.title);
            if (mTitleView != null) {
                mTitleView.setLayoutDirection(mDecor.getLayoutDirection());
                if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
                    View titleContainer = findViewById(
                            R.id.title_container);
                    if (titleContainer != null) {
                        titleContainer.setVisibility(View.GONE);
                    } else {
                        mTitleView.setVisibility(View.GONE);
                    }
                    if (mContentParent instanceof FrameLayout) {
                        ((FrameLayout)mContentParent).setForeground(null);
                    }
                } else {
                    mTitleView.setText(mTitle);
                }
            }
        }

        if (mDecor.getBackground() == null && mBackgroundFallbackResource != 0) {
            mDecor.setBackgroundFallback(mBackgroundFallbackResource);
        }

        // Only inflate or create a new TransitionManager if the caller hasn't
        // already set a custom one.
        if (hasFeature(FEATURE_ACTIVITY_TRANSITIONS)) {
            if (mTransitionManager == null) {
                final int transitionRes = getWindowStyle().getResourceId(
                        R.styleable.Window_windowContentTransitionManager,
                        0);
                if (transitionRes != 0) {
                    final TransitionInflater inflater = TransitionInflater.from(getContext());
                    mTransitionManager = inflater.inflateTransitionManager(transitionRes,
                            mContentParent);
                } else {
                    mTransitionManager = new TransitionManager();
                }
            }

            mEnterTransition = getTransition(mEnterTransition, null,
                    R.styleable.Window_windowEnterTransition);
            mReturnTransition = getTransition(mReturnTransition, USE_DEFAULT_TRANSITION,
                    R.styleable.Window_windowReturnTransition);
            mExitTransition = getTransition(mExitTransition, null,
                    R.styleable.Window_windowExitTransition);
            mReenterTransition = getTransition(mReenterTransition, USE_DEFAULT_TRANSITION,
                    R.styleable.Window_windowReenterTransition);
            mSharedElementEnterTransition = getTransition(mSharedElementEnterTransition, null,
                    R.styleable.Window_windowSharedElementEnterTransition);
            mSharedElementReturnTransition = getTransition(mSharedElementReturnTransition,
                    USE_DEFAULT_TRANSITION,
                    R.styleable.Window_windowSharedElementReturnTransition);
            mSharedElementExitTransition = getTransition(mSharedElementExitTransition, null,
                    R.styleable.Window_windowSharedElementExitTransition);
            mSharedElementReenterTransition = getTransition(mSharedElementReenterTransition,
                    USE_DEFAULT_TRANSITION,
                    R.styleable.Window_windowSharedElementReenterTransition);
            if (mAllowEnterTransitionOverlap == null) {
                mAllowEnterTransitionOverlap = getWindowStyle().getBoolean(
                        R.styleable.Window_windowAllowEnterTransitionOverlap, true);
            }
            if (mAllowReturnTransitionOverlap == null) {
                mAllowReturnTransitionOverlap = getWindowStyle().getBoolean(
                        R.styleable.Window_windowAllowReturnTransitionOverlap, true);
            }
            if (mBackgroundFadeDurationMillis < 0) {
                mBackgroundFadeDurationMillis = getWindowStyle().getInteger(
                        R.styleable.Window_windowTransitionBackgroundFadeDuration,
                        DEFAULT_BACKGROUND_FADE_DURATION_MS);
            }
            if (mSharedElementsUseOverlay == null) {
                mSharedElementsUseOverlay = getWindowStyle().getBoolean(
                        R.styleable.Window_windowSharedElementsUseOverlay, true);
            }
        }
    }
}

我们先看一下generateLayout的源码:

protected ViewGroup generateLayout(DecorView decor) {
    // Apply data from current theme.

    TypedArray a = getWindowStyle();

    if (false) {
        System.out.println("From style:");
        String s = "Attrs:";
        for (int i = 0; i < R.styleable.Window.length; i++) {
            s = s + " " + Integer.toHexString(R.styleable.Window[i]) + "="
                    + a.getString(i);
        }
        System.out.println(s);
    }

    mIsFloating = a.getBoolean(R.styleable.Window_windowIsFloating, false);
    int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR)
            & (~getForcedWindowFlags());
    if (mIsFloating) {
        setLayout(WRAP_CONTENT, WRAP_CONTENT);
        setFlags(0, flagsToUpdate);
    } else {
        setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate);
    }

    if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
        requestFeature(FEATURE_NO_TITLE);
    } else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) {
        // Don't allow an action bar if there is no title.
        requestFeature(FEATURE_ACTION_BAR);
    }

    if (a.getBoolean(R.styleable.Window_windowActionBarOverlay, false)) {
        requestFeature(FEATURE_ACTION_BAR_OVERLAY);
    }

    if (a.getBoolean(R.styleable.Window_windowActionModeOverlay, false)) {
        requestFeature(FEATURE_ACTION_MODE_OVERLAY);
    }

    if (a.getBoolean(R.styleable.Window_windowSwipeToDismiss, false)) {
        requestFeature(FEATURE_SWIPE_TO_DISMISS);
    }

    if (a.getBoolean(R.styleable.Window_windowFullscreen, false)) {
        setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN & (~getForcedWindowFlags()));
    }

    if (a.getBoolean(R.styleable.Window_windowTranslucentStatus,
            false)) {
        setFlags(FLAG_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS
                & (~getForcedWindowFlags()));
    }

    if (a.getBoolean(R.styleable.Window_windowTranslucentNavigation,
            false)) {
        setFlags(FLAG_TRANSLUCENT_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION
                & (~getForcedWindowFlags()));
    }

    if (a.getBoolean(R.styleable.Window_windowOverscan, false)) {
        setFlags(FLAG_LAYOUT_IN_OVERSCAN, FLAG_LAYOUT_IN_OVERSCAN&(~getForcedWindowFlags()));
    }

    if (a.getBoolean(R.styleable.Window_windowShowWallpaper, false)) {
        setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags()));
    }

    if (a.getBoolean(R.styleable.Window_windowEnableSplitTouch,
            getContext().getApplicationInfo().targetSdkVersion
                    >= android.os.Build.VERSION_CODES.HONEYCOMB)) {
        setFlags(FLAG_SPLIT_TOUCH, FLAG_SPLIT_TOUCH&(~getForcedWindowFlags()));
    }

    a.getValue(R.styleable.Window_windowMinWidthMajor, mMinWidthMajor);
    a.getValue(R.styleable.Window_windowMinWidthMinor, mMinWidthMinor);
    if (a.hasValue(R.styleable.Window_windowFixedWidthMajor)) {
        if (mFixedWidthMajor == null) mFixedWidthMajor = new TypedValue();
        a.getValue(R.styleable.Window_windowFixedWidthMajor,
                mFixedWidthMajor);
    }
    if (a.hasValue(R.styleable.Window_windowFixedWidthMinor)) {
        if (mFixedWidthMinor == null) mFixedWidthMinor = new TypedValue();
        a.getValue(R.styleable.Window_windowFixedWidthMinor,
                mFixedWidthMinor);
    }
    if (a.hasValue(R.styleable.Window_windowFixedHeightMajor)) {
        if (mFixedHeightMajor == null) mFixedHeightMajor = new TypedValue();
        a.getValue(R.styleable.Window_windowFixedHeightMajor,
                mFixedHeightMajor);
    }
    if (a.hasValue(R.styleable.Window_windowFixedHeightMinor)) {
        if (mFixedHeightMinor == null) mFixedHeightMinor = new TypedValue();
        a.getValue(R.styleable.Window_windowFixedHeightMinor,
                mFixedHeightMinor);
    }
    if (a.getBoolean(R.styleable.Window_windowContentTransitions, false)) {
        requestFeature(FEATURE_CONTENT_TRANSITIONS);
    }
    if (a.getBoolean(R.styleable.Window_windowActivityTransitions, false)) {
        requestFeature(FEATURE_ACTIVITY_TRANSITIONS);
    }

    final WindowManager windowService = (WindowManager) getContext().getSystemService(
            Context.WINDOW_SERVICE);
    if (windowService != null) {
        final Display display = windowService.getDefaultDisplay();
        final boolean shouldUseBottomOutset =
                display.getDisplayId() == Display.DEFAULT_DISPLAY
                        || (getForcedWindowFlags() & FLAG_FULLSCREEN) != 0;
        if (shouldUseBottomOutset && a.hasValue(R.styleable.Window_windowOutsetBottom)) {
            if (mOutsetBottom == null) mOutsetBottom = new TypedValue();
            a.getValue(R.styleable.Window_windowOutsetBottom,
                    mOutsetBottom);
        }
    }

    final Context context = getContext();
    final int targetSdk = context.getApplicationInfo().targetSdkVersion;
    final boolean targetPreHoneycomb = targetSdk < android.os.Build.VERSION_CODES.HONEYCOMB;
    final boolean targetPreIcs = targetSdk < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
    final boolean targetPreL = targetSdk < android.os.Build.VERSION_CODES.LOLLIPOP;
    final boolean targetHcNeedsOptions = context.getResources().getBoolean(
            R.bool.target_honeycomb_needs_options_menu);
    final boolean noActionBar = !hasFeature(FEATURE_ACTION_BAR) || hasFeature(FEATURE_NO_TITLE);

    if (targetPreHoneycomb || (targetPreIcs && targetHcNeedsOptions && noActionBar)) {
        addFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);
    } else {
        clearFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);
    }

    // Non-floating windows on high end devices must put up decor beneath the system bars and
    // therefore must know about visibility changes of those.
    if (!mIsFloating && ActivityManager.isHighEndGfx()) {
        if (!targetPreL && a.getBoolean(
                R.styleable.Window_windowDrawsSystemBarBackgrounds,
                false)) {
            setFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
                    FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS & ~getForcedWindowFlags());
        }
    }
    if (!mForcedStatusBarColor) {
        mStatusBarColor = a.getColor(R.styleable.Window_statusBarColor, 0xFF000000);
    }
    if (!mForcedNavigationBarColor) {
        mNavigationBarColor = a.getColor(R.styleable.Window_navigationBarColor, 0xFF000000);
    }

    if (mAlwaysReadCloseOnTouchAttr || getContext().getApplicationInfo().targetSdkVersion
            >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        if (a.getBoolean(
                R.styleable.Window_windowCloseOnTouchOutside,
                false)) {
            setCloseOnTouchOutsideIfNotSet(true);
        }
    }

    WindowManager.LayoutParams params = getAttributes();

    if (!hasSoftInputMode()) {
        params.softInputMode = a.getInt(
                R.styleable.Window_windowSoftInputMode,
                params.softInputMode);
    }

    if (a.getBoolean(R.styleable.Window_backgroundDimEnabled,
            mIsFloating)) {
        /* All dialogs should have the window dimmed */
        if ((getForcedWindowFlags()&WindowManager.LayoutParams.FLAG_DIM_BEHIND) == 0) {
            params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        }
        if (!haveDimAmount()) {
            params.dimAmount = a.getFloat(
                    android.R.styleable.Window_backgroundDimAmount, 0.5f);
        }
    }

    if (params.windowAnimations == 0) {
        params.windowAnimations = a.getResourceId(
                R.styleable.Window_windowAnimationStyle, 0);
    }

    // The rest are only done if this window is not embedded; otherwise,
    // the values are inherited from our container.
    if (getContainer() == null) {
        if (mBackgroundDrawable == null) {
            if (mBackgroundResource == 0) {
                mBackgroundResource = a.getResourceId(
                        R.styleable.Window_windowBackground, 0);
            }
            if (mFrameResource == 0) {
                mFrameResource = a.getResourceId(R.styleable.Window_windowFrame, 0);
            }
            mBackgroundFallbackResource = a.getResourceId(
                    R.styleable.Window_windowBackgroundFallback, 0);
            if (false) {
                System.out.println("Background: "
                        + Integer.toHexString(mBackgroundResource) + " Frame: "
                        + Integer.toHexString(mFrameResource));
            }
        }
        mElevation = a.getDimension(R.styleable.Window_windowElevation, 0);
        mClipToOutline = a.getBoolean(R.styleable.Window_windowClipToOutline, false);
        mTextColor = a.getColor(R.styleable.Window_textColor, Color.TRANSPARENT);
    }

    // 上面的那一块都是对Activity中设置的主题进行判断。下面就是根据不同的主题,选择不同的布局文件。
    // Inflate the window decor.

    int layoutResource;
    int features = getLocalFeatures();
    // System.out.println("Features: 0x" + Integer.toHexString(features));
    if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
        layoutResource = R.layout.screen_swipe_dismiss;
    } else if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
        if (mIsFloating) {
            TypedValue res = new TypedValue();
            getContext().getTheme().resolveAttribute(
                    R.attr.dialogTitleIconsDecorLayout, res, true);
            layoutResource = res.resourceId;
        } else {
            layoutResource = R.layout.screen_title_icons;
        }
        // XXX Remove this once action bar supports these features.
        removeFeature(FEATURE_ACTION_BAR);
        // System.out.println("Title Icons!");
    } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0
            && (features & (1 << FEATURE_ACTION_BAR)) == 0) {
        // Special case for a window with only a progress bar (and title).
        // XXX Need to have a no-title version of embedded windows.
        layoutResource = R.layout.screen_progress;
        // System.out.println("Progress!");
    } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
        // Special case for a window with a custom title.
        // If the window is floating, we need a dialog layout
        if (mIsFloating) {
            TypedValue res = new TypedValue();
            getContext().getTheme().resolveAttribute(
                    R.attr.dialogCustomTitleDecorLayout, res, true);
            layoutResource = res.resourceId;
        } else {
            layoutResource = R.layout.screen_custom_title;
        }
        // XXX Remove this once action bar supports these features.
        removeFeature(FEATURE_ACTION_BAR);
    } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
        // If no other features and not embedded, only need a title.
        // If the window is floating, we need a dialog layout
        if (mIsFloating) {
            TypedValue res = new TypedValue();
            getContext().getTheme().resolveAttribute(
                    R.attr.dialogTitleDecorLayout, res, true);
            layoutResource = res.resourceId;
        } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) {
            layoutResource = a.getResourceId(
                    R.styleable.Window_windowActionBarFullscreenDecorLayout,
                    R.layout.screen_action_bar);
        } else {
            layoutResource = R.layout.screen_title;
        }
        // System.out.println("Title!");
    } else if ((features & (1 << FEATURE_ACTION_MODE_OVERLAY)) != 0) {
        layoutResource = R.layout.screen_simple_overlay_action_mode;
    } else {
        // Embedded, so no decoration is needed.
        layoutResource = R.layout.screen_simple;
        // System.out.println("Simple!");
    }

    mDecor.startChanging();

    // inflate对应的布局文件,并添加到mDecor中。
    View in = mLayoutInflater.inflate(layoutResource, null);
    decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
    mContentRoot = (ViewGroup) in;

    // 找到布局中android:id="@android:id/content"。的ViewGroup赋值给contentParent,一般是FrameLayout
    ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
    if (contentParent == null) {
        throw new RuntimeException("Window couldn't find content container view");
    }

    if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
        ProgressBar progress = getCircularProgressBar(false);
        if (progress != null) {
            progress.setIndeterminate(true);
        }
    }

    if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
        registerSwipeCallbacks();
    }

    // Remaining setup -- of background and title -- that only applies
    // to top-level windows.
    if (getContainer() == null) {
        final Drawable background;
        if (mBackgroundResource != 0) {
            background = getContext().getDrawable(mBackgroundResource);
        } else {
            background = mBackgroundDrawable;
        }
        mDecor.setWindowBackground(background);

        final Drawable frame;
        if (mFrameResource != 0) {
            frame = getContext().getDrawable(mFrameResource);
        } else {
            frame = null;
        }
        mDecor.setWindowFrame(frame);

        mDecor.setElevation(mElevation);
        mDecor.setClipToOutline(mClipToOutline);

        if (mTitle != null) {
            setTitle(mTitle);
        }

        if (mTitleColor == 0) {
            mTitleColor = mTextColor;
        }
        setTitleColor(mTitleColor);
    }

    mDecor.finishChanging();

    return contentParent;
}   

上面看到有很多相应主题的布局文件,我们就以典型的R.layout.screen_title为例看一下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <!-- Popout bar for action modes -->
    <ViewStub android:id="@+id/action_mode_bar_stub"
              android:inflatedId="@+id/action_mode_bar"
              android:layout="@layout/action_mode_bar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="?attr/actionBarTheme" />
    // 标题
    <FrameLayout
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/windowTitleSize"
        style="?android:attr/windowTitleBackgroundStyle">
        <TextView android:id="@android:id/title" 
            style="?android:attr/windowTitleStyle"
            android:background="@null"
            android:fadingEdge="horizontal"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    // 页面内容
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

看到这里基本差不多了,我们就以第一次调用setContentView方法为例总结一下整体的流程。
- 第一次调用setContentView()方法时会去创建一个DecorView,这就是整个窗口的根布局。
- 接着回去根据我们设置的对应主题,来加载与之对应的布局文件并将其添加到DecorView中,然后找到该布局中id=content的ViewGroup赋值给mContentParent。
- 将我们要设置的Activity对应的布局添加到mContentParent中。


  • 邮箱 :charon.chui@gmail.com
  • Good Luck!
Activity 启动流程至view绘制到屏幕显示 Activity 启动 至 view绘制 至 屏幕显示 实际效果:Activity冷启动的时候会闪黑屏,就是A启动B,这个过程中会闪黑屏,虽然网上说可以设置一个闪屏页,中间有个过渡效果,或者设置B为透明主题,这样在启动的时候就看不到黑屏了。只是感觉启动B的这个过程漫长而已,又比如说你从桌面启动A,你把A设为透明,你就会感觉你点击A之后半天没反应没启动起来,实际是有反应的哇,因为你设置的透明,所以... 阅读详情

相关推荐

流程图(Activity)绘制

概述 流程是完成一系列有序动作的概述。每一个节点动作的结果将对后面的具体操作步骤产生影响。信息化系统中流程的功能完全等同于纸上办公的层级审批,尤其在oa系统中各类电子流提现较为明显。一般的步骤为: ① 申请者发起申请。 ② 各级领导审批。一般由低级别往高级别审批。 ③ 每一级别审批结果将影响或者决定申请结果。若下一节点非结束节点,此节点若审批通过将转给下一节点审批;若此节点审批不通过此...

Chief_fly的博客 7979

5分钟告诉你,Activity的视图绘制流程(onMeasure、onLayout、onDraw的调用和参数解释)

本篇主要面对有一定Android基础的同学,但是Android初学者的话,也不要急。在网上可以看到很多文章,会告诉你onMeaure是什么?里面的参数是什么意思?会教你怎么获取MeaSpec的size和mode(Unspecified、Exactly、AtMost)。也会告诉你怎么重写 onLayout,怎么使用layout方法去布局子元素,怎么实现流式布局,自动换行。还有会告诉你重写onDraw...

王智博 2651

Activity启动过程详解(Android P)

本章我们来分析Activity的启动过程,我们先从熟悉的startActivity方法来开始分析。 startActivity的实现 要启动一个Activity,无论是从Lanuch启动,还是App内部启动,都需要调用startActivity()方法来实现。 startActivity定义 startAcrtivity方法是在Context中定义的: public abstract vo......

卜大爷的博客 2万+

Android源码解析(十八)-->Activity布局绘制流程

这篇文章是承接上一篇文章来写的,大家都知道Activity在Android体系中扮演者一个界面展示的角色,通过上一篇文章的分析,我们知道Activity是通过Window来控制界面的展示的,一个Window对象就是一个窗口对象,而每个Activity中都有一个相应的Window对象,所以说一个Activity对象也就可以说是一个窗口对象,而Window只是控制着界面布局文件的加载过程,那么界面布局

一片枫叶的专栏 1万+

activiti工作流程图绘制工具web版

activiti工作流程图绘制工具web版,可直接放到tomcat中使用,默认账户/密码为kermit/kermit

Activity工作流在线绘制流程图

分享一个web端绘制流程图,感觉很实用。下载解压后直接导入eclipse,运行方式 HTTP Preview,访问地址http://localhost:8080/estd/index.html 希望能帮助到大家。

【Activity】绘制流程图方式(部署,启动不讨论)

activity绘制流程图大概有三种方式(可能还有其他的,我不知道,但这三种是最长用的) 归结起来份两类: 第一类:利用开发工具插件绘制(eclipse、idea等) 第二类:activity自带的ActivitiModeler 下面详细介绍 第一类:开发工具插件绘制 idea中安装插件(我的版本2020.2.3) 在idea中安装插件actiBpm(新版本下载不到) 如果搜索不到可在idea官网中搜索插件下载 在官方插件商城中下载后本地安装...

一步一个脚印_binding 6859

电气比例阀采用外置传感器和PID控制器实现化学机械抛光(CMP)的超高精度压力控制

摘要:为大幅度提高现有CMP工艺设备中压力控制的稳定性,在现有电气比例阀这种单回路PID压力调节技术的基础上,本文提出了升级改造方案,即采用串级控制法(双回路PID控制,也称级联控制),通过在现有电气比例阀回路中增加更高精度的压力传感器和PID控制器,可以将研磨抛光压力的稳定性提高一个数量级,从1~2%的稳定性提升到0.1~0.2%。

Shanghai_Eyoung的博客 1292

Activity界面绘制流程

onCreate中完成view的创建

Kai的博客 939

半导体设备微振动控制:天棚阻尼PID与SIMULINK建模实战

半导体制造设备对微振动的敏感度已进入纳米级,光刻机、量测设备等一旦遭遇超过VC-C振动准则的扰动,便会出现套刻误差或检测误判。主动隔振技术通过传感器、控制器与执行器形成闭环,能有效抑制低频共振,其中天棚阻尼控制因物理概念清晰、鲁棒性强而成为工程首选。结合MATLAB/SIMULINK建模,可在设计阶段完成系统性能边界评估与参数整定,再通过C代码生成与FMU导出实现算法向控制器和跨平台仿真迁移。本文从VC-C指标拆解出发,依次讲解天棚阻尼PID控制原理、两自由度被控对象建模、控制增益估算、仿真验证方法,并给出

weixin_29169505的博客 326

RANSAC地面点分割:从原理到C++实战优化

本文深入解析RANSAC算法在点云地面分割中的原理与C++实战优化,涵盖动态迭代次数、自适应阈值等关键技巧,通过并行计算和内存访问优化提升性能,适用于激光雷达点云处理。

weixin_29015051的博客 280

Android Activity启动过程详解

ctivity启动的流程分为两部分:一是在activity中通过startActivity(Intent intent)方法启动一个Activity;二是我们在桌面通过点击应用图标启动一个App然后显示Activity;第二种方式相较于第一种方式更加全面和复杂,所以本文会以第二种流程来分析。应用的启动流程大致分为下面五个阶段:1,应用启动进程 Launcher 调用 ATMS 系统进程的过程2,ATMS 向 AMS 发送创建应用进程的过程。

allen_xu_2012_new的博客 1万+

从零构建:基于Docker与FRR的OSPF虚拟网络实验室

本文详细介绍了如何利用Docker与FRR构建OSPF虚拟网络实验室,从环境设计到高级网络拓扑配置,再到实验验证与排错。通过容器化技术实现高效资源利用和快速部署,特别适合网络协议教学、企业网络架构验证等场景。

plum99的博客 223

Activity界面的加载和绘制

初探窗口在知道Activity的生命周期调用时机之后,这些都太抽象了,到底我们看到的界面是怎么加载出来的才是最感兴趣的,这里面就主要涉及Activity的Window对象了,具体是什么机制呢?

梦里梦到醒不来的梦 2124

19. OTA功能 - 1

耳机采用AB分区的OTA方式,AB分区详见分区表,通过手机蓝牙下发更新文件。

雪飞_海 650

教师模型与学生模型:知识蒸馏的核心概念

知识蒸馏,教师模型,学生模型,模型压缩,迁移学习,深度学习 1. 背景介绍 深度学习模型在图像识别、自然语言处理等领域取得了显著的成就,但其巨大的模型规模和高昂的计算成本限制了其在资源有限的设备上的部署。知识蒸馏(Knowledge Distillation)作为一种模型压缩技术,旨

AI架构师小马 2772

从源码角度看一个activity的绘制过程

我们都知道一个activity的绘制过程是从setContentView方法开始的 那么setContentView背后到底干了些什么不为人知的事情,今天让我们来揭开它神秘的面纱,我们来看看setContentView的源码 public void setContentView(@LayoutRes int layoutResID) { getWindow().setContent

salute_li的博客 593

向华为学习——解读158页 华为敏捷园区网技术解决方案【附全文阅读】

华为敏捷园区网解决方案通过策略随行和体验随身技术,满足移动办公场景下的网络需求。该方案采用敏捷交换机和防火墙自动执行策略配置,实现用户身份认证与业务策略的无缝衔接,且策略配置与IP地址解耦,简化了管理流程。相比传统技术,该方案有效解决了用户分级、QoS保障、无线接入等关键问题,确保用户在移动过程中获得一致的网络体验。

cdfunlove的博客 381

Activity页面的绘制流程

一.引言 很多朋友都认为Activity的页面绘制是在Activity的onResume方法执行完成就被成功绘制成功了,认为这个时候我们就可以看到我们写的页面了。那真的是这样嘛?其实并不然,Activity的页面它制是在Activity的onResume方法执行完成后才会被绘制,且绘制完成的时长与我们主线当时的状态和布局的层级以及CPU内存都是有一定的关系,这些也是我们分析Activity启动黑屏...

braintt的博客 5619

27、单阶段目标检测算法(SSD):原理、训练与实战

本文详细介绍了单阶段目标检测算法SSD的原理、训练方法与实战应用。内容涵盖SSD的核心机制,如特征图离散化、边界框预测、损失函数设计及难负样本挖掘,并通过使用dlib车辆数据集从零开始训练SSD模型的完整流程展示了实际操作步骤。文章还讨论了训练中的关键问题,如边界框坐标处理和‘忽略区域’缺失的影响,提供了模型评估与优化策略,包括超参数调整、基础网络替换和数据增强。最后总结了SSD的优势与局限性,并展望了未来目标检测技术的发展方向。

nice1的博客 508

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Android 从Activity创建开始,绘制View的过程

1. 在Activity中的Activity的attach方法中

houyu_1983的专栏 6228

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

android 界面绘制过程

1. View与ViewGroup类关系        Android View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的。View是基类,ViewGroup是它的子类。这就证明了一点,View代表了用户界面组件的一块可绘制的空间块。每一个View在屏幕上占据一个长方形区域。在这个区域内,这个VIEW对象负责图

nate1860的专栏 1255

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

android 界面绘制

首先laingc几个类:

u012370421的专栏 600

Activity启动流程,界面绘制到事件处理的整个流程(基于Android6.0源码)(2)

二、界面的绘制流程 上一节把Activity的启动流程介绍了一下,在Activity的启动流程中,onResume的生命周期是显示界面的过程,下面来分析界面是如何显示的。 PhoneWindow并不是具体的布局界面,我们可以看到handleResumeActivity中,有下面代码调用 final void handleResumeActivity(IBinder token,

newhope1106的博客 2143

Activity启动及窗口创建过程详解

有这么一个网站,能通过web方式查看Android源码.在你需要的时候,本地又没有源码的时候,通过这个网站可以很方便的查看到源码网站地址:http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android1. 入口。 以前一直都说Activity的人口是onCreate方法。其实androi

小码农大梦想 1355
上一篇: 自定义控件
下一篇: Android Touch事件分发详解
CharonChui
博客等级 码龄12年 66粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值