zoukankan      html  css  js  c++  java
  • onAttachedToWindow () 和 onDetachedFromWindow () ; 以及更新视图的函数ondraw() 和dispatchdraw()的区别

    protected void onAttachedToWindow()

     This is called when the view is attached to a window. At this point it has a Surface and will    start drawing. Note that this function is guaranteed to be called  before onDraw(android.graphics.Canvas), however it may be called any time before the first  onDraw -- including before or after onMeasure(int, int).


    如果你在自己的view中Override了这个方法。那么我们最关注的是它什么时候调用?

    从开发文档中我们可以看出,onAttachedToWindow是在第一次onDraw前调用的。也就是我们写的View在没有绘制出来时调用的,但只会调用一次。

    比如,我们写状态栏中的时钟的View,在onAttachedToWindow这方法中做初始化工作,比如注册一些广播等等……

    与onAttachedToWindow 相反的则是这个方法:

    protected void onDetachedFromWindow()

    Since: API Level 1

    This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

    开发文档就简单的两句。也就是我们销毁View的时候。我们写的这个View不再显示。

    这时我们就在这个方法做一些收尾工作,如:取消广播注册等等。

    ===========================================================================================

    关于在Activity中什么时候调用onAttachedToWindow()和onDetachedFromWindow(),我通常测试打了下log,发现onAttachedToWindow()在onResume()之后运行,onDetachedFromWindow()则在onDestory()之后才会调用。

    =========================================================================================================

    更新视图的函数onDraw()和dispatchdraw()的区别

     

    绘制VIew本身的内容,通过调用View.onDraw(canvas)函数实现

    绘制自己的孩子通过dispatchDraw(canvas)实现 

    View组件的绘制会调用draw(Canvas canvas)方法,draw过程中主要是先画Drawable背景,对 drawable调用setBounds(),然后是draw(Canvas c)方法。有点注意的是背景drawable的实际大小会影响view组件的大小,drawable的实际大小通过getIntrinsicWidth()和getIntrinsicHeight()获取,当背景比较大时view组件大小等于背景drawable的大小。

    画完背景后,draw过程会调用onDraw(Canvas canvas)方法,然后就是dispatchDraw(Canvas canvas)方法,dispatchDraw()主要是分发给子组件进行绘制,我们通常定制组件的时候重写的是onDraw()方法。值得注意的是ViewGroup容器组件的绘制,当它没有背景时直接调用的是dispatchDraw()方法, 而绕过了draw()方法,当它有背景的时候就调用draw()方法,而draw()方法里包含了dispatchDraw()方法的调用。因此要在ViewGroup上绘制东西的时候往往重写的是dispatchDraw()方法而不是onDraw()方法,或者自定制一个Drawable,重写它的draw(Canvas c)和 getIntrinsicWidth(),getIntrinsicHeight()方法,然后设为背景。

    本文转自:http://blog.csdn.net/vipclx/article/details/8078431

  • 相关阅读:
    ruby 二进制转十进制 Integer("0b101") = 5
    开始菜单和我的文档的我的图片及我的音乐变成 my pictrues 正常图标了
    ruby watir 莫名其妙的错误
    Excel SaveAS是去掉提示框
    apache && jboss安装
    ruby require include的区别
    ruby控制鼠标
    This error is raised because the column 'type' is reserved for storing the class in case of inheritance
    用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/3958949.html
Copyright © 2011-2022 走看看