Android开发经验谈Android实用技术

2019日更挑战(五),Android--聊聊View的绘制

2019-01-05  本文已影响87人  Jlanglang

瞎扯

android中所有控件的基类,其重要性不用说了.
装逼必备
简历上加上擅长自定义view,实现各种牛逼效果.
是不是瞬间感觉有逼格了.

View做了什么.

记得刚写Android的时候.不怎么看源码
那时我就认为.屏幕展示出来的东西,就是view这个类实现出来的,觉得很神奇.
然后看源码,也是各种类,不明所以.
后来,我才明白,跟想的完全不是一回事


View绘制

实际上,之所以能在界面上看到不同的View.
真正做事的.并不是View.

是谁呢?

Canvas

我们能在屏幕上看到内容.都是Canvas画出来的.
不管是图片也好.背景也好.控件.动画也好

那View做了什么呢?

持有绘制参数 绘制规则.然后通过Canvas.绘制出来的.

参数指什么:

就是view中保存各种属性参数.padding,margin,Width,Height等等自定义的参数

规则指什么

常说的3大方法.
onLayout onDraw onMeasure 这些让你复写的.还有自定义的方法.
作用,无非就是改变View的参数属性而已

View的绘制时机

让view的刷新的方法,invalidate.
如下:

    public void invalidate() {
        invalidate(true);
    }

    void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
            boolean fullInvalidate) {
        //省略..
            final AttachInfo ai = mAttachInfo;
            final ViewParent p = mParent;
            
            //这里获取是否有父级.通知绘制
            if (p != null && ai != null && l < r && t < b) {
                final Rect damage = ai.mTmpInvalRect;
                damage.set(l, t, r, b);
                p.invalidateChild(this, damage);
            }
          //省略..
        }
    }

但是,看完发现.除了通知父级以外.根本找不到任务调用onDraw的代码

再看:

    /**
     * Manually render this view (and all of its children) to the given Canvas.
     * The view must have already done a full layout before this function is
     * called.  When implementing a view, implement
     * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
     * If you do need to override this method, call the superclass version.
     *
     * @param canvas The Canvas to which the View is rendered.
     */
    @CallSuper
    public void draw(Canvas canvas) {
    //....省略
      // Step 3, draw the content
            if (!dirtyOpaque) onDraw(canvas);

            // Step 4, draw the children
            dispatchDraw(canvas);

            drawAutofilledHighlight(canvas);
    //....省略
}

ViewGroup的:

   protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        return child.draw(canvas, this, drawingTime);
    }

这里才调用了实际的onDraw方法.

那invalidate是如何操作回调用到draw()的呢.
代码太多了.
一张图可以搞定.不过是通过异常来看的

  java.lang.ArithmeticException: divide by zero
        at com.example.jlanglang.myapplication.TestView.onDraw(TestView.java:66)
        at android.view.View.draw(View.java:20497)
        at android.view.View.updateDisplayListIfDirty(View.java:19308)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4375)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4348)
        at android.view.View.updateDisplayListIfDirty(View.java:19268)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4375)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4348)
        at android.view.View.updateDisplayListIfDirty(View.java:19268)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4375)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4348)
        at android.view.View.updateDisplayListIfDirty(View.java:19268)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4375)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4348)
        at android.view.View.updateDisplayListIfDirty(View.java:19268)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4375)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4348)
        at android.view.View.updateDisplayListIfDirty(View.java:19268)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4375)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4348)
        at android.view.View.updateDisplayListIfDirty(View.java:19268)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:729)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:735)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:850)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:3586)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3370)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2701)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1624)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7926)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1079)
        at android.view.Choreographer.doCallbacks(Choreographer.java:885)
        at android.view.Choreographer.doFrame(Choreographer.java:809)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1065)

大致就是,调用后,会优先通知到ViewRootImpl,然后层层递归到View.调用onDraw进行绘制

不过,不用担心页面是否一个控件改变,整个页面的View都重新绘制.有判断的.


Canvas的子类--DisplayListCanvas

既然页面上的所以东西都是Canvas一次一次画出来的.

那我们平常用的Canvas,要在上面画东西,是需要Bitmap的.

难道每个界面都会创建一层Bitmap?
我是不是可以拿到onDraw()传进来的Canvas.改掉这个Bitmap?

哈哈,天真

public final class DisplayListCanvas extends RecordingCanvas {
    //...省略
   @Override
    public void setBitmap(Bitmap bitmap) {
        throw new UnsupportedOperationException();
    }
}
image.png

开发的人早就想到了这点.所以写了这个子类.

这个DisplayListCanvasRenderNode初始化创建的.

    public View(Context context) {
        mRenderNode = RenderNode.create(getClass().getName(), this);
  }
   public DisplayListCanvas start(int width, int height) {
        return DisplayListCanvas.obtain(this, width, height);
    }
  static DisplayListCanvas obtain(@NonNull RenderNode node, int width, int height) {
        if (node == null) throw new IllegalArgumentException("node cannot be null");
        DisplayListCanvas canvas = sPool.acquire();
        if (canvas == null) {
            canvas = new DisplayListCanvas(node, width, height);
        } else {
            nResetDisplayListCanvas(canvas.mNativeCanvasWrapper, node.mNativeRenderNode,
                    width, height);
        }
        canvas.mNode = node;
        canvas.mWidth = width;
        canvas.mHeight = height;
        return canvas;
    }

RenderNode是啥?

    /**
     * RenderNode holding View properties, potentially holding a DisplayList of View content.
     * <p>
     * When non-null and valid, this is expected to contain an up-to-date copy
     * of the View content. Its DisplayList content is cleared on temporary detach and reset on
     * cleanup.
     */
    final RenderNode mRenderNode;

每个View都会有这玩意.你说干啥的,绘制的呗.

所以DisplayListCanvas,基本就是专门绘制布局的Canvas.


canvas如何实现绘制的:

自己看,


image.png

其实Canvas也就是一个链接底层c的jni交互类而已.具体实现调用硬件还是c


总结:

今天周末,稍微多写点东西,好像也没写什么,还是一堆废话,哈哈

大致想说的就是.
所有的界面展示出来的东西,都是canvas画的

View的源码看起来吓人,实际上.
如果只看头尾,不去管那些隐藏的方法逻辑

其实就几个注意的地方:

1.onDraw的参数canvasDisplayListCanvas
2.只有调用了invalidate之类的方法,才会刷新布局
3.View真正有绘制逻辑是draw(),onDraw只是给我们复写的.
4.invalidate调用,就像事件分发一样.会一直往上到ViewRootImpl.然后再往下到需要绘制的view的Preant,再通过view的引用调用draw()

就这样吧.日更第5天.


您的喜欢与回复是我最大的动力-_-
交流群:493180098

上一篇 下一篇

猜你喜欢

热点阅读