在重写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);
}
具体的用法视个人的需求而定了,自己控制重写就好了。
分享到:
相关推荐
强大的控件重写,其他的不说了,直接上代码!
由于项目中需要使用到ratingbar,可之前每次使用总有各种各样的限制 使用过的小伙伴可能会有所体会,这次需要设置ratingbar之间的间距,我试了很多种方法都无效 迫于无奈,只能自己写一个呗 现在分享出来给小伙伴们...
Android 重写Gallery实现gallery的动画切换效果
<com.triman.android.ui.MyRadioButton android:id="@+id/rb_sys_set" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:...
通常情况下,Android实现自定义控件无非三种方式。 Ⅰ、继承现有控件,对其控件的功能进行拓展。 Ⅱ、将现有控件进行组合,实现功能更加强大控件。 Ⅲ、重写View实现全新的控件 本文来讨论最难的一种自定义...
Android 应用开发源码 参考与学习使用
Android PopupWindow重写系统菜单
android自定义view之组合控件、重写控件
里面代码可以直接运行,Android截屏功能实现,重写View类似QQ截屏,非自带截屏,主要是ImageTatilor这个类的实现,对屏幕截取有兴趣的可以研究研究一下。
Android 重写 Camera类 自定义拍照和摄像功能 不是简单调用系统的Intent,可以设置多个参数 拍照分辨率,摄像的视频格式,适用于不同场合,非常实用,对于系统Intent不能满足需求的同学 可以参考 欢迎留言评论哈
Android自定义view组合控件、重写控件的例子源码,创建出对应的组合布局视图,将创建出的视图添加到根节点。Android使用view控件的例子,面向Android开发新手,高手绕行。
android 重写控件添加自定义属性,具体可见http://blog.csdn.net/zenmela2011/article/details/42459481
android应用源码Android重写listview实现下拉刷新.zip源码资源下载android应用源码Android重写listview实现下拉刷新.zip源码资源下载
android 重写的table
主要介绍了Android重写View并自定义属性的方法,结合实例形式较为详细的分析了Android基于重写View实现自定义属性的相关布局与具体技巧,需要的朋友可以参考下
android 各种常用控件介绍以及重写控件增加新功能的方法,高手进阶
重写了Dialog,使Dialog类似于一个数字键盘,顺便附带了跳出“正在保存...”的对话框,都是重写了Dialog。 具体可以参考:http://blog.csdn.net/jasonzhou613/article/details/7905388
android应用源码Android重写listview实现下拉刷新源码,android安卓实例应用源代码,仅供学习及设计参考。