android phone 模块分析,Android Surface模块分析(一)

每天一篇系列:

强化知识体系,查漏补缺。

欢迎指正,共同学习!

目标1:Activity的显示和Surface是如何关联上的?

一般来说,你能看到的一个窗口就是一个Surface,在之前发表过的文章中也介绍过Activity,Window,View之间的关系,大概可以理解为一个Activity创建出一个可视化界面,而这个界面就可以理解为一个Surface,Activity在这个Surface上不断绘制,从而看到Android终端上绚丽的界面。

在之前文章中分析过ActivityThread通过反射创建了一个Activity,并且调用这个Activity的OnCreat函数:

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {

...

Activity activity = null;

try {

java.lang.ClassLoader cl = r.packageInfo.getClassLoader();

activity = mInstrumentation.newActivity(

cl, component.getClassName(), r.intent);

StrictMode.incrementExpectedActivityCount(activity.getClass());

r.intent.setExtrasClassLoader(cl);

r.intent.prepareToEnterProcess();

if (r.state != null) {

r.state.setClassLoader(cl);

}

} catch (Exception e) {

if (!mInstrumentation.onException(activity, e)) {

throw new RuntimeException(

"Unable to instantiate activity " + component

+ ": " + e.toString(), e);

}

}

try {

Application app = r.packageInfo.makeApplication(false, mInstrumentation);

if (localLOGV) Slog.v(TAG, "Performing launch of " + r);

if (localLOGV) Slog.v(

TAG, r + ": app=" + app

+ ", appName=" + app.getPackageName()

+ ", pkg=" + r.packageInfo.getPackageName()

+ ", comp=" + r.intent.getComponent().toShortString()

+ ", dir=" + r.packageInfo.getAppDir());

if (activity != null) {

Context appContext = createBaseContextForActivity(r, activity);

CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());

Configuration config = new Configuration(mCompatConfiguration);

if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "

+ r.activityInfo.name + " with config " + config);

activity.attach(appContext, this, getInstrumentation(), r.token,

r.ident, app, r.intent, r.activityInfo, title, r.parent,

r.embeddedID, r.lastNonConfigurationInstances, config,

r.voiceInteractor);

if (customIntent != null) {

activity.mIntent = customIntent;

}

r.lastNonConfigurationInstances = null;

activity.mStartedActivity = false;

int theme = r.activityInfo.getThemeResource();

if (theme != 0) {

activity.setTheme(theme);

}

activity.mCalled = false;

if (r.isPersistable()) {

mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);

} else {

mInstrumentation.callActivityOnCreate(activity, r.state);

}

if (!activity.mCalled) {

throw new SuperNotCalledException(

"Activity " + r.intent.getComponent().toShortString() +

" did not call through to super.onCreate()");

}

r.activity = activity;

r.stopped = true;

if (!r.activity.mFinished) {

activity.performStart();

r.stopped = false;

}

if (!r.activity.mFinished) {

if (r.isPersistable()) {

if (r.state != null || r.persistentState != null) {

mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,

r.persistentState);

}

} else if (r.state != null) {

mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);

}

}

if (!r.activity.mFinished) {

activity.mCalled = false;

if (r.isPersistable()) {

mInstrumentation.callActivityOnPostCreate(activity, r.state,

r.persistentState);

} else {

mInstrumentation.callActivityOnPostCreate(activity, r.state);

}

if (!activity.mCalled) {

throw new SuperNotCalledException(

"Activity " + r.intent.getComponent().toShortString() +

" did not call through to super.onPostCreate()");

}

}

}

r.paused = true;

mActivities.put(r.token, r);

} catch (Exception e) {

if (!mInstrumentation.onException(activity, e)) {

throw new RuntimeException(

"Unable to start activity " + component

+ ": " + e.toString(), e);

}

}

return activity;

}

也简单的提到过在Activity的attach方法中创建过Window:

final void attach(Context context, ActivityThread aThread,

Instrumentation instr, IBinder token, int ident,

Application application, Intent intent, ActivityInfo info,

CharSequence title, Activity parent, String id,

NonConfigurationInstances lastNonConfigurationInstances,

Configuration config, IVoiceInteractor voiceInteractor) {

attachBaseContext(context);

mFragments.attachActivity(this, mContainer, null);

mWindow = PolicyManager.makeNewWindow(this);

mWindow.setCallback(this);

mWindow.setOnWindowDismissedCallback(this);

mWindow.getLayoutInflater().setPrivateFactory(this);

if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {

mWindow.setSoftInputMode(info.softInputMode);

}

if (info.uiOptions != 0) {

mWindow.setUiOptions(info.uiOptions);

}

mUiThread = Thread.currentThread();

mMainThread = aThread;

mInstrumentation = instr;

mToken = token;

mIdent = ident;

mApplication = application;

mIntent = intent;

mComponent = intent.getComponent();

mActivityInfo = info;

mTitle = title;

mParent = parent;

mEmbeddedID = id;

mLastNonConfigurationInstances = lastNonConfigurationInstances;

if (voiceInteractor != null) {

if (lastNonConfigurationInstances != null) {

mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;

} else {

mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,

Looper.myLooper());

}

}

mWindow.setWindowManager(

(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),

mToken, mComponent.flattenToString(),

(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);

if (mParent != null) {

mWindow.setContainer(mParent.getWindow());

}

mWindowManager = mWindow.getWindowManager();

mCurrentConfig = config;

}

这里的Window是本篇文章的主角。

mWindow = PolicyManager.makeNewWindow(this);

创建了一个Window对象,PolicyManager其实只是一个包装类:

public static Window makeNewWindow(Context context) {

return sPolicy.makeNewWindow(context);

}

通过反射创建一个PhoneWindow对象,PhoneWindow对象是继承于Window的,这里隐约已经可以把Activity和Window联系起来了:

public Window makeNewWindow(Context context) {

return new PhoneWindow(context);

}

也就是说Activity创建时就创建了一个PhoneWindow,同时也通过父类Window的setCallback接口让PhoneWindow保存了对Activity的引用:

/**

* Set the Callback interface for this window, used to intercept key

* events and other dynamic operations in the window.

*

* @param callback The desired Callback interface.

*/

public void setCallback(Callback callback) {

mCallback = callback;

}

/**

* Return the current Callback interface for this window.

*/

public final Callback getCallback() {

return mCallback;

}

接下来又通过mToken设置了WindowManager,这个mToken的作用类似句柄用来通信使用?

public void setWindowManager(WindowManager wm, IBinder appToken, String appName,

boolean hardwareAccelerated) {

mAppToken = appToken;

mAppName = appName;

mHardwareAccelerated = hardwareAccelerated

|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);

if (wm == null) {

wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);

}

mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);

}

创建的WindowManager实际上是WindowManagerImpl:

private WindowManagerImpl(Display display, Window parentWindow) {

mDisplay = display;

mParentWindow = parentWindow;

}

public WindowManagerImpl createLocalWindowManager(Window parentWindow) {

return new WindowManagerImpl(mDisplay, parentWindow);

}

至于使用需要在文章后面才能说明。

是否还记的曾经在哪里提及到创建了DetorView?

在PhoneWindow里有一个内部类DetorView:

private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {

/* package */int mDefaultOpacity = PixelFormat.OPAQUE;

/** The feature ID of the panel, or -1 if this is the application's DecorView */

private final int mFeatureId;

private final Rect mDrawingBounds = new Rect();

private final Rect mBackgroundPadding = new Rect();

private final Rect mFramePadding = new Rect();

private final Rect mFrameOffsets = new Rect();

...

}

回过头去看performLaunchActivity函数,完成activity.attach之后,会调用mInstrumentation.callActivityOnCreate:

public void callActivityOnCreate(Activity activity, Bundle icicle,

PersistableBundle persistentState) {

prePerformCreate(activity);

activity.performCreate(icicle, persistentState);

postPerformCreate(activity);

}

final void performCreate(Bundle icicle, PersistableBundle persistentState) {

onCreate(icicle, persistentState);

mActivityTransitionState.readState(icicle);

performCreateCommon();

}

其中onCreate就会调用应用中Activity的onCreate,开发者重写了onCreate,并且会在onCreate方法里显示调用setContentView,这个时候就会创建DecorView:

/**Activity.java*/

public void setContentView(View view, ViewGroup.LayoutParams params) {

//这里的getWindow就是创建的PhoneWindow对象

getWindow().setContentView(view, params);

initWindowDecorActionBar();

}

/**PhoneWindow.java*/

public void setContentView(int layoutResID) {

...

if (mContentParent == null) {

installDecor();

} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {

mContentParent.removeAllViews();

}

...

}

private void installDecor() {

if (mDecor == null) {

mDecor = generateDecor();

mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

mDecor.setIsRootNamespace(true);

if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {

mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);

}

}

...

}

}

}

}

DetorView是PhoneWindow的内部类,继承Framelayout,包含了一个TitleView和ContentView,而PhoneWindow又继承于Window(内部类可以无限制的访问外部类的属性和方法),因此在DecorView里的cb就是承载了这个窗口的Activity对象。

现在可以总结为:在performLaunchActivity阶段创建了Window,并且看到已经有一个DetorView涉及到应用的UI了,但是我们还是不了解Window和Surface又有什么关系,UI又是如何通过Window在Surface上实现绘制的?

final void handleResumeActivity(IBinder token,

boolean clearHide, boolean isForward, boolean reallyResume) {

...

if (r.window == null && !a.mFinished && willBeVisible) {

r.window = r.activity.getWindow();

View decor = r.window.getDecorView();

decor.setVisibility(View.INVISIBLE);

ViewManager wm = a.getWindowManager();

WindowManager.LayoutParams l = r.window.getAttributes();

a.mDecor = decor;

l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;

l.softInputMode |= forwardBit;

if (a.mVisibleFromClient) {

a.mWindowAdded = true;

wm.addView(decor, l);

}

// If the window has already been added, but during resume

// we started another activity, then don't yet make the

// window visible.

} else if (!willBeVisible) {

if (localLOGV) Slog.v(

TAG, "Launch " + r + " mStartedActivity set");

r.hideForNow = true;

}

...

}

到这里我们看到一个在UI开发中特别熟悉的API:addView,r.window是在attach阶段创建的PhoneWindow,而wm是在attach阶段创建的WindowManager,确切的说是WindowManagerImpl:

private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

@Override

public void addView(View view, ViewGroup.LayoutParams params) {

mGlobal.addView(view, params, mDisplay, mParentWindow);

}

是的,这里又涉及到一个新的对象WindowManagerGlobal:

public final class WindowManagerGlobal {

...

private static WindowManagerGlobal sDefaultWindowManager;

private static IWindowManager sWindowManagerService;

private static IWindowSession sWindowSession;

private WindowManagerGlobal() {

}

...

//看到这里,不知道为什么我总想改成静态内部类的单例模式或者双重检查的单例模式

public static WindowManagerGlobal getInstance() {

synchronized (WindowManagerGlobal.class) {

if (sDefaultWindowManager == null) {

sDefaultWindowManager = new WindowManagerGlobal();

}

return sDefaultWindowManager;

}

}

...

public void addView(View view, ViewGroup.LayoutParams params,

Display display, Window parentWindow) {

...

root = new ViewRootImpl(view.getContext(), display);

view.setLayoutParams(wparams);

mViews.add(view);

mRoots.add(root);

mParams.add(wparams);

}

// do this last because it fires off messages to start doing things

try {

root.setView(view, wparams, panelParentView);

} catch (RuntimeException e) {

// BadTokenException or InvalidDisplayException, clean up.

synchronized (mLock) {

final int index = findViewLocked(view, false);

if (index >= 0) {

removeViewLocked(index, true);

}

}

throw e;

}

}

...

}

为了简化分析,按添加第一个DecorView来梳理流程,首先parentWindow为null,并且从没有添加到Window中,所以核心代码就只剩下ViewRootImpl的创建以及setView,ViewRootImpl终于现身了:

public final class ViewRootImpl implements ViewParent,

View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks {

...

private final Surface mSurface = new Surface();

...

public ViewRootImpl(Context context, Display display) {

mContext = context;

mWindowSession = WindowManagerGlobal.getWindowSession();

...

}

/**

* We have one child

*/

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {

synchronized (this) {

if (mView == null) {

mView = view;

...

requestLayout();

if ((mWindowAttributes.inputFeatures

& WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {

mInputChannel = new InputChannel();

}

...

if (mInputChannel != null) {

if (mInputQueueCallback != null) {

mInputQueue = new InputQueue();

mInputQueueCallback.onInputQueueCreated(mInputQueue);

}

mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,

Looper.myLooper());

}

...

// Set up the input pipeline.

CharSequence counterSuffix = attrs.getTitle();

mSyntheticInputStage = new SyntheticInputStage();

InputStage viewPostImeStage = new ViewPostImeInputStage(mSyntheticInputStage);

InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage,

"aq:native-post-ime:" + counterSuffix);

InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);

InputStage imeStage = new ImeInputStage(earlyPostImeStage,

"aq:ime:" + counterSuffix);

InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);

InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage,

"aq:native-pre-ime:" + counterSuffix);

mFirstInputStage = nativePreImeStage;

mFirstPostImeInputStage = earlyPostImeStage;

mPendingInputEventQueueLengthCounterName = "aq:pending:" + counterSuffix;

}

}

}

在setView阶段除了添加View以外,还创建了和Input的关联,本篇不分析事件分发的内容。

转了大半天终于看到Surface的身影了。梳理下思路,首先在启动一个应用的Activity时,在启动阶段创建Window,在onCreat阶段通过setContentView首先创建一个DetorView,并在addView阶段把DetorView添加到ViewRootImpl中,也在同时创建了一个Surface。

requestLayout();

核心函数出现了。这个函数真正意义上和Surface开始沟通:

@Override

public void requestLayout() {

if (!mHandlingLayoutInLayoutRequest) {

checkThread();

mLayoutRequested = true;

scheduleTraversals();

}

}

void checkThread() {

//看到了吧 为什么总是强调对UI的操作要在UI线程

if (mThread != Thread.currentThread()) {

throw new CalledFromWrongThreadException(

"Only the original thread that created a view hierarchy can touch its views.");

}

}

void scheduleTraversals() {

if (!mTraversalScheduled) {

mTraversalScheduled = true;

mTraversalBarrier = mHandler.getLooper().postSyncBarrier();

//实际上是在UI线程去执行TraversalRunnable的run函数

mChoreographer.postCallback(

Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);

if (!mUnbufferedInputDispatch) {

scheduleConsumeBatchedInput();

}

notifyRendererOfFramePending();

}

}

final class TraversalRunnable implements Runnable {

@Override

public void run() {

doTraversal();

}

}

final TraversalRunnable mTraversalRunnable = new TraversalRunnable();

void doTraversal() {

if (mTraversalScheduled) {

mTraversalScheduled = false;

mHandler.getLooper().removeSyncBarrier(mTraversalBarrier);

if (mProfile) {

Debug.startMethodTracing("ViewAncestor");

}

Trace.traceBegin(Trace.TRACE_TAG_VIEW, "performTraversals");

try {

performTraversals();

} finally {

Trace.traceEnd(Trace.TRACE_TAG_VIEW);

}

if (mProfile) {

Debug.stopMethodTracing();

mProfile = false;

}

}

}

到这一步的流程都很好理解,但是接下来performTraversals就复杂了,做好心理准备:

private void performTraversals() {

...

// Ask host how big it wants to be

performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

...

performLayout(lp, desiredWindowWidth, desiredWindowHeight);

...

performDraw();

...

}

看到了Android绘制流程的三个核心函数,其中performDraw流程直接关系到Surface的绘制:

private void performDraw() {

...

try {

draw(fullRedrawNeeded);

} finally {

mIsDrawing = false;

Trace.traceEnd(Trace.TRACE_TAG_VIEW);

}

...

}

private void draw(boolean fullRedrawNeeded) {

Surface surface = mSurface;

...

if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {

return;

}

}

}

if (animating) {

mFullRedrawNeeded = true;

scheduleTraversals();

}

}

接下来就是通过Surface获取到Canvas,并且在Canvas上绘制了:

private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,

boolean scalingRequired, Rect dirty) {

// Draw with software renderer.

final Canvas canvas;

try {

final int left = dirty.left;

final int top = dirty.top;

final int right = dirty.right;

final int bottom = dirty.bottom;

//要开始使用最熟悉的canvas来绘制了

canvas = mSurface.lockCanvas(dirty);

// The dirty rectangle can be modified by Surface.lockCanvas()

//noinspection ConstantConditions

if (left != dirty.left || top != dirty.top || right != dirty.right

|| bottom != dirty.bottom) {

attachInfo.mIgnoreDirtyState = true;

}

// TODO: Do this in native

canvas.setDensity(mDensity);

} catch (Surface.OutOfResourcesException e) {

handleOutOfResourcesException(e);

return false;

} catch (IllegalArgumentException e) {

Log.e(TAG, "Could not lock surface", e);

// Don't assume this is due to out of memory, it could be

// something else, and if it is something else then we could

// kill stuff (or ourself) for no reason.

mLayoutRequested = true; // ask wm for a new surface next time.

return false;

}

try {

if (DEBUG_ORIENTATION || DEBUG_DRAW) {

Log.v(TAG, "Surface " + surface + " drawing to bitmap w="

+ canvas.getWidth() + ", h=" + canvas.getHeight());

//canvas.drawARGB(255, 255, 0, 0);

}

// If this bitmap's format includes an alpha channel, we

// need to clear it before drawing so that the child will

// properly re-composite its drawing on a transparent

// background. This automatically respects the clip/dirty region

// or

// If we are applying an offset, we need to clear the area

// where the offset doesn't appear to avoid having garbage

// left in the blank areas.

if (!canvas.isOpaque() || yoff != 0 || xoff != 0) {

canvas.drawColor(0, PorterDuff.Mode.CLEAR);

}

dirty.setEmpty();

mIsAnimating = false;

attachInfo.mDrawingTime = SystemClock.uptimeMillis();

mView.mPrivateFlags |= View.PFLAG_DRAWN;

if (DEBUG_DRAW) {

Context cxt = mView.getContext();

Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +

", metrics=" + cxt.getResources().getDisplayMetrics() +

", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());

}

try {

canvas.translate(-xoff, -yoff);

if (mTranslator != null) {

mTranslator.translateCanvas(canvas);

}

canvas.setScreenDensity(scalingRequired ? mNoncompatDensity : 0);

attachInfo.mSetIgnoreDirtyState = false;

//最终回调mView的draw方法,这里在分析时不就是DecorView嘛

mView.draw(canvas);

} finally {

if (!attachInfo.mSetIgnoreDirtyState) {

// Only clear the flag if it was not set during the mView.draw() call

attachInfo.mIgnoreDirtyState = false;

}

}

} finally {

try {

//绘制结束后释放canvas

surface.unlockCanvasAndPost(canvas);

} catch (IllegalArgumentException e) {

Log.e(TAG, "Could not unlock surface", e);

mLayoutRequested = true; // ask wm for a new surface next time.

//noinspection ReturnInsideFinallyBlock

return false;

}

if (LOCAL_LOGV) {

Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");

}

}

return true;

}

DecorView继承FrameLayout,DecorView未重写draw方法,根据继承和多态特性,FrameLayout的处理如下:

@Override

public void draw(Canvas canvas) {

super.draw(canvas);

if (mForeground != null) {

final Drawable foreground = mForeground;

if (mForegroundBoundsChanged) {

mForegroundBoundsChanged = false;

final Rect selfBounds = mSelfBounds;

final Rect overlayBounds = mOverlayBounds;

final int w = mRight-mLeft;

final int h = mBottom-mTop;

if (mForegroundInPadding) {

selfBounds.set(0, 0, w, h);

} else {

selfBounds.set(mPaddingLeft, mPaddingTop, w - mPaddingRight, h - mPaddingBottom);

}

final int layoutDirection = getLayoutDirection();

Gravity.apply(mForegroundGravity, foreground.getIntrinsicWidth(),

foreground.getIntrinsicHeight(), selfBounds, overlayBounds,

layoutDirection);

foreground.setBounds(overlayBounds);

}

foreground.draw(canvas);

}

}

首先回调了父类ViewGroup的draw函数,ViewGroup继承于View,且ViewGroup没有重写draw方法,那么会回调到View的draw方法内:

public void draw(Canvas canvas) {

final int privateFlags = mPrivateFlags;

final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&

(mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);

mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

/*

* Draw traversal performs several drawing steps which must be executed

* in the appropriate order:

*

* 1. Draw the background

* 2. If necessary, save the canvas' layers to prepare for fading

* 3. Draw view's content

* 4. Draw children

* 5. If necessary, draw the fading edges and restore layers

* 6. Draw decorations (scrollbars for instance)

*/

// Step 1, draw the background, if needed

int saveCount;

if (!dirtyOpaque) {

drawBackground(canvas);

}

// skip step 2 & 5 if possible (common case)

final int viewFlags = mViewFlags;

boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;

boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;

if (!verticalEdges && !horizontalEdges) {

// Step 3, draw the content

if (!dirtyOpaque) onDraw(canvas);

// Step 4, draw the children

dispatchDraw(canvas);

// Step 6, draw decorations (scrollbars)

onDrawScrollBars(canvas);

if (mOverlay != null && !mOverlay.isEmpty()) {

mOverlay.getOverlayView().dispatchDraw(canvas);

}

// we're done...

return;

}

...

}

真的是够详细了....,我们按最普遍绘制情况下的处理来分析,先去看是否需要绘制背景有则绘制,然后回调onDraw方法,并且一层一层绘制子类View,最后如果需要绘制滑动条时去绘制滑动条。

onDraw方法只在DecorView中重写:

@Override

public void onDraw(Canvas c) {

super.onDraw(c);

mBackgroundFallback.draw(mContentRoot, c, mContentParent);

}

这个函数是老老实实的去绘制每个需要显示子类View的背景图:

public void draw(ViewGroup root, Canvas c, View content) {

if (!hasFallback()) {

return;

}

// Draw the fallback in the padding.

final int width = root.getWidth();

final int height = root.getHeight();

int left = width;

int top = height;

int right = 0;

int bottom = 0;

final int childCount = root.getChildCount();

for (int i = 0; i < childCount; i++) {

final View child = root.getChildAt(i);

final Drawable childBg = child.getBackground();

if (child == content) {

// We always count the content view container unless it has no background

// and no children.

if (childBg == null && child instanceof ViewGroup &&

((ViewGroup) child).getChildCount() == 0) {

continue;

}

} else if (child.getVisibility() != View.VISIBLE || childBg == null ||

childBg.getOpacity() != PixelFormat.OPAQUE) {

// Potentially translucent or invisible children don't count, and we assume

// the content view will cover the whole area if we're in a background

// fallback situation.

continue;

}

left = Math.min(left, child.getLeft());

top = Math.min(top, child.getTop());

right = Math.max(right, child.getRight());

bottom = Math.max(bottom, child.getBottom());

}

if (left >= right || top >= bottom) {

// No valid area to draw in.

return;

}

if (top > 0) {

mBackgroundFallback.setBounds(0, 0, width, top);

mBackgroundFallback.draw(c);

}

if (left > 0) {

mBackgroundFallback.setBounds(0, top, left, height);

mBackgroundFallback.draw(c);

}

if (right < width) {

mBackgroundFallback.setBounds(right, top, width, height);

mBackgroundFallback.draw(c);

}

if (bottom < height) {

mBackgroundFallback.setBounds(left, bottom, right, height);

mBackgroundFallback.draw(c);

}

}

而dispatchDraw只在ViewGroup中重写:

/**

* {@inheritDoc}

*/

@Override

protected void dispatchDraw(Canvas canvas) {

boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);

final int childrenCount = mChildrenCount;

final View[] children = mChildren;

int flags = mGroupFlags;

if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {

final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;

final boolean buildCache = !isHardwareAccelerated();

for (int i = 0; i < childrenCount; i++) {

final View child = children[i];

if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {

final LayoutParams params = child.getLayoutParams();

attachLayoutAnimationParameters(child, params, i, childrenCount);

bindLayoutAnimation(child);

if (cache) {

child.setDrawingCacheEnabled(true);

if (buildCache) {

child.buildDrawingCache(true);

}

}

}

}

final LayoutAnimationController controller = mLayoutAnimationController;

if (controller.willOverlap()) {

mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;

}

controller.start();

mGroupFlags &= ~FLAG_RUN_ANIMATION;

mGroupFlags &= ~FLAG_ANIMATION_DONE;

if (cache) {

mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;

}

if (mAnimationListener != null) {

mAnimationListener.onAnimationStart(controller.getAnimation());

}

}

int clipSaveCount = 0;

final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;

if (clipToPadding) {

clipSaveCount = canvas.save();

canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,

mScrollX + mRight - mLeft - mPaddingRight,

mScrollY + mBottom - mTop - mPaddingBottom);

}

// We will draw our child's animation, let's reset the flag

mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;

mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;

boolean more = false;

final long drawingTime = getDrawingTime();

if (usingRenderNodeProperties) canvas.insertReorderBarrier();

// Only use the preordered list if not HW accelerated, since the HW pipeline will do the

// draw reordering internally

final ArrayList preorderedList = usingRenderNodeProperties

? null : buildOrderedChildList();

final boolean customOrder = preorderedList == null

&& isChildrenDrawingOrderEnabled();

for (int i = 0; i < childrenCount; i++) {

int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;

final View child = (preorderedList == null)

? children[childIndex] : preorderedList.get(childIndex);

if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {

more |= drawChild(canvas, child, drawingTime);

}

}

if (preorderedList != null) preorderedList.clear();

// Draw any disappearing views that have animations

if (mDisappearingChildren != null) {

final ArrayList disappearingChildren = mDisappearingChildren;

final int disappearingCount = disappearingChildren.size() - 1;

// Go backwards -- we may delete as animations finish

for (int i = disappearingCount; i >= 0; i--) {

final View child = disappearingChildren.get(i);

more |= drawChild(canvas, child, drawingTime);

}

}

if (usingRenderNodeProperties) canvas.insertInorderBarrier();

if (debugDraw()) {

onDebugDraw(canvas);

}

if (clipToPadding) {

canvas.restoreToCount(clipSaveCount);

}

// mGroupFlags might have been updated by drawChild()

flags = mGroupFlags;

if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {

invalidate(true);

}

if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&

mLayoutAnimationController.isDone() && !more) {

// We want to erase the drawing cache and notify the listener after the

// next frame is drawn because one extra invalidate() is caused by

// drawChild() after the animation is over

mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;

final Runnable end = new Runnable() {

public void run() {

notifyAnimationListener();

}

};

post(end);

}

}

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {

return child.draw(canvas, this, drawingTime);

}

在这个阶段如果有动画效果的话,需要去重新通过drawChild绘制所有需要显示的子View。如果需要绘制scrollbars时在onDrawScrollBars方法内绘制出滚动条。

有了画布Canvas(这里说的时Java层的Canvas),就顺理成章的可以绘制我们需要UI,Surface模块分析二讲继续分析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值