Android重写view时onAttachedToWindow () 和 onDetachedFromWindow ()
在重写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就是在这个方法中进行广播的注册,代码如下:
- @Override
- protected void onAttachedToWindow() {
- super.onAttachedToWindow();
- if (Log.LOGV) Log.v("onAttachedToWindow " + this);
- if (mAttached) return;
- mAttached = true;
- if (mAnimate) {
- setBackgroundResource(R.drawable.animate_circle);
- /* Start the animation (looped playback by default). */
- ((AnimationDrawable) getBackground()).start();
- }
- if (mLive) {
- /* monitor time ticks, time changed, timezone */
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_TIME_TICK);
- filter.addAction(Intent.ACTION_TIME_CHANGED);
- filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
- mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
- }
- /* monitor 12/24-hour display preference */
- mFormatChangeObserver = new FormatChangeObserver();
- mContext.getContentResolver().registerContentObserver(
- Settings.System.CONTENT_URI, true, mFormatChangeObserver);
- updateTime();
- }
另外在屏蔽Home键的时候也会用到
- public void onAttachedToWindow() {
- this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
- super.onAttachedToWindow();
- }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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的闹钟代码:
- @Override
- protected void onDetachedFromWindow() {
- super.onDetachedFromWindow();
- if (!mAttached) return;
- mAttached = false;
- Drawable background = getBackground();
- if (background instanceof AnimationDrawable) {
- ((AnimationDrawable) background).stop();
- }
- if (mLive) {
- mContext.unregisterReceiver(mIntentReceiver);
- }
- mContext.getContentResolver().unregisterContentObserver(
- mFormatChangeObserver);
- }
具体的用法视个人的需求而定了,自己控制重写就好了。
Android重写view时onAttachedToWindow () 和 onDetachedFromWindow ()的更多相关文章
- android重写view和viewgroup的区别
重写view: View类一般用于绘图操作,重写它的onDraw方法,但它不可以包含其他组件,没有addView(View view)方法. 重写viewgroup: ViewGroup是一个组件容器 ...
- Android 自定义View 三板斧之三——重写View来实现全新控件
通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 本文来讨论最难的一种 ...
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
这里只做个汇总~.~独一无二 文章出处:http://blog.csdn.net/djy1992/article/details/9715047 Android自定义View研究--View中的原点坐 ...
- Android Studio 重写方法时参数命名异常
Android Studio 重写方法时参数命名异常 Android Studio 重写方法时参数名称乱掉可以通过下载相应源码解决
- android 自定义view详解
1.自定义View前首先要了解一下View的方法,虽然有些不一定要实现. 分类 方法 描述 创建 Constructors View中有两种类型的构造方法,一种是在代码中构建View,另一种是填充布局 ...
- onAttachedToWindow () 和 onDetachedFromWindow () ; 以及更新视图的函数ondraw() 和dispatchdraw()的区别
protected void onAttachedToWindow() This is called when the view is attached to a window. At this po ...
- 每日一问:浅谈 onAttachedToWindow 和 onDetachedFromWindow
基本上所有 Android 开发都会接触到 onCreate().onDestory().onStart().onStop() 等这些生命周期方法,但却不是所有人都会去关注到 onAttachXXX( ...
- Android之View的内容
View的事件体系 本章介绍View的事件分发和滑动冲突问题的解决方案. 3.1 view的基础知识 View的位置参数.MotionEvent和TouchSlop对象.VelocityTracker ...
- Android之view的工作原理2
学习内容 View的底层工作原理,比如View的测量流程.布局流程以及绘制流程:以及常见的View回调方法:熟悉掌握前面的知识后,自定义View的时候也会更加的得心应手. 4.1 初识ViewRoot ...
随机推荐
- 12864点阵液晶显示模块的原理和实例程序(HJ12864M-1)
12864点阵液晶显示模块(LCM)就是由 128*64个液晶显示点组成的一个128列*64行的阵列.每个显示点对应一位二进制数,1表示亮,0表示灭.存储这些点阵信息的RAM称为显示数据存 储器.要显 ...
- HDU 1561 The more, The Better(树形背包)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 中国首届CSS开发者大会讲师照片
中国首届CSS开发者大会讲师照片 Bert Bos Winter 点头猪 灭灭 jaychsu Hax 尤雨溪 一丝 勾三股四 小倩 **
- Leetcode27--->Remove Element(移除数组中给定元素)
题目:给定一个数组array和一个值value,移除掉数组中所有与value值相等的元素,返回新的数组的长度:要求:不能分配额外的数组空间,且必须使用原地排序的思想,空间复杂度O(1); 举例: Gi ...
- LiveScript 操作符
The LiveScript Book The LiveScript Book 操作符 数字 标准的数学操作符: 1.1 + 2 # => 32.3 - 4 # => -13.6 ...
- Leetcode 416.分割等和子集
分割等和子集 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会超过 200 示例 1: 输入: [ ...
- TOJ4203: Domino Piece
4203: Domino Piece Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 5 ...
- Linux Shell系列教程之(五)Shell字符串
本文是Linux Shell系列教程的第(五)篇,更多shell教程请看:Linux Shell系列教程 字符串是Shell编程中最常用最有用的数据类型,今天,Linux大学网就为大家介绍一下在She ...
- windows10下安装ubuntu16.04 双系统
软件和材料: UltraISO .ubuntu16.04镜像.U盘 步骤: 1.先在windows10 上下载UltraISO并安装 2.打开UltraISO,插入优盘,制作ubuntu160.4 ...
- Linq技巧1——关联实体查询排序
假如想查询拖欠按揭超过30天的银行帐号,同时查询出他们的单据,并且需要按照单据日期进行排序,这样可以首先看到最近的单据,方便找出问题. 大多数人都知道EF可以使用Include()热加载关系实体,例如 ...