在重写View的时候,会遇到这两个方法

protected void onAttachedToWindow()

Description copied from class: View

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 View.onDraw(Android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after View.onMeasure(int, int).
Overrides:
onAttachedToWindow in class View

当此view附加到窗体上时调用该方法。在这时,view有了一个用于显示的Surface,将开始绘制。注意,此方法要保证在调用onDraw(Canvas)
之前调用,但可能在调用 onDraw(Canvas) 之前的任何时刻,包括调用 onMeasure(int, int)
之前或之后。

看得出次方法在onDraw方法之前调用,也就是view还没有画出来的时候,可以在此方法中去执行一些初始化的操作,google的AlarmClock动态时钟View就是在这个方法中进行广播的注册,代码如下:

  1. @Override
  2. protected void onAttachedToWindow() {
  3. super.onAttachedToWindow();
  4. if (Log.LOGV) Log.v("onAttachedToWindow " + this);
  5. if (mAttached) return;
  6. mAttached = true;
  7. if (mAnimate) {
  8. setBackgroundResource(R.drawable.animate_circle);
  9. /* Start the animation (looped playback by default). */
  10. ((AnimationDrawable) getBackground()).start();
  11. }
  12. if (mLive) {
  13. /* monitor time ticks, time changed, timezone */
  14. IntentFilter filter = new IntentFilter();
  15. filter.addAction(Intent.ACTION_TIME_TICK);
  16. filter.addAction(Intent.ACTION_TIME_CHANGED);
  17. filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
  18. mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
  19. }
  20. /* monitor 12/24-hour display preference */
  21. mFormatChangeObserver = new FormatChangeObserver();
  22. mContext.getContentResolver().registerContentObserver(
  23. Settings.System.CONTENT_URI, true, mFormatChangeObserver);
  24. updateTime();
  25. }

另外在屏蔽Home键的时候也会用到

  1. public void onAttachedToWindow() {
  2. this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
  3. super.onAttachedToWindow();
  4. }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

protected void onDetachedFromWindow()

Description copied from class: View
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
Overrides:
onDetachedFromWindow in class AdapterView<ListAdapter>

将视图从窗体上分离的时候调用该方法。这时视图已经不具有可绘制部分。

onDetachedFromWindow()正好与onAttachedToWindow()的用法相对应,在destroy view的时候调用,所以可以加入取消广播注册等的操作,还是google的闹钟代码:

  1. @Override
  2. protected void onDetachedFromWindow() {
  3. super.onDetachedFromWindow();
  4. if (!mAttached) return;
  5. mAttached = false;
  6. Drawable background = getBackground();
  7. if (background instanceof AnimationDrawable) {
  8. ((AnimationDrawable) background).stop();
  9. }
  10. if (mLive) {
  11. mContext.unregisterReceiver(mIntentReceiver);
  12. }
  13. mContext.getContentResolver().unregisterContentObserver(
  14. mFormatChangeObserver);
  15. }

具体的用法视个人的需求而定了,自己控制重写就好了。

Android重写view时onAttachedToWindow () 和 onDetachedFromWindow ()的更多相关文章

  1. android重写view和viewgroup的区别

    重写view: View类一般用于绘图操作,重写它的onDraw方法,但它不可以包含其他组件,没有addView(View view)方法. 重写viewgroup: ViewGroup是一个组件容器 ...

  2. Android 自定义View 三板斧之三——重写View来实现全新控件

    通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 本文来讨论最难的一种 ...

  3. Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题

    这里只做个汇总~.~独一无二 文章出处:http://blog.csdn.net/djy1992/article/details/9715047 Android自定义View研究--View中的原点坐 ...

  4. Android Studio 重写方法时参数命名异常

    Android Studio 重写方法时参数命名异常 Android Studio 重写方法时参数名称乱掉可以通过下载相应源码解决

  5. android 自定义view详解

    1.自定义View前首先要了解一下View的方法,虽然有些不一定要实现. 分类 方法 描述 创建 Constructors View中有两种类型的构造方法,一种是在代码中构建View,另一种是填充布局 ...

  6. onAttachedToWindow () 和 onDetachedFromWindow () ; 以及更新视图的函数ondraw() 和dispatchdraw()的区别

    protected void onAttachedToWindow() This is called when the view is attached to a window. At this po ...

  7. 每日一问:浅谈 onAttachedToWindow 和 onDetachedFromWindow

    基本上所有 Android 开发都会接触到 onCreate().onDestory().onStart().onStop() 等这些生命周期方法,但却不是所有人都会去关注到 onAttachXXX( ...

  8. Android之View的内容

    View的事件体系 本章介绍View的事件分发和滑动冲突问题的解决方案. 3.1 view的基础知识 View的位置参数.MotionEvent和TouchSlop对象.VelocityTracker ...

  9. Android之view的工作原理2

    学习内容 View的底层工作原理,比如View的测量流程.布局流程以及绘制流程:以及常见的View回调方法:熟悉掌握前面的知识后,自定义View的时候也会更加的得心应手. 4.1 初识ViewRoot ...

随机推荐

  1. linux学习-主机的细部权限规划:ACL 的使用

    传统的权限仅有三种身份 (owner, group, others) 搭配三种权限 (r,w,x) 而已,并没有办法单纯的针对某一个使用者或某一个群 组来设定特定的权限需求,此时就得要使用 ACL 这 ...

  2. BZOJ 3257: 树的难题

    树形DP #include<cstdio> #include<algorithm> #define rep(i,x,y) for (int i=x; i<=y; i++) ...

  3. UVa 12235 状压DP Help Bubu

    题解戳这 一开始没看懂题解,后来想明白以后,d(i, j, s, x)是考虑第i本书的时候,前面已经拿走了j本书,剩下的书的种类的二进制状态为s,剩下的最后一本书的编号为x,所能得到的最小混乱度. 这 ...

  4. loj2028 「SHOI2016」随机序列

    定义区间是内部只含有乘号的区间. 对于区间左端点是 \(l \geq 2\) 的情况,左端点前头是加号的情况和前头是减号的情况的个数是相同的.因此这些区间不对答案产生贡献. 所以区间左端点必定是 \( ...

  5. 关于MySQL建表对DML的影响【转】

    本文来自这里 今天一位同学问到线上曾经碰到过连续建表,导致阻塞普通的insert.update等.不过也没有保留现场.因此有疑问为什么建表会影响DML? 分析          首先这个现象不是在所有 ...

  6. 如何利用App打造自明星实现自盈利

    1.了解各个概念      为了大家都能看懂这篇文章,先说明几个概念.       App(Application):可以在移动设备上使用,满足人们咨询.购物.社交.娱乐.搜索等需求的一切应用程序.  ...

  7. 《百词斩·象形9000》第一册(下) 符号Symbol 1

    001-version n.版本:译文 This is the first version of the software #这是软件开发的第一个版本: 002-element n.成分:要素:元素: ...

  8. JAVA-字符串按指定长度换行

    可能有汉字的字符串按指定长度换行. public String getStringByEnter(int length, String string) throws Exception { for ( ...

  9. 分区脚本(fdisk)

    #!/bin/bash echo "np w" | fdisk /dev/sdc && mkfs -t /dev/sdc1

  10. Golang遇到的问题记录

    1,windows cmd 结束输入问题 func main() { counts := make(map[string]int) countLines(os.Stdin, counts) fmt.P ...