参考:
http://blog.sina.com.cn/s/blog_4b50130d0100u0uk.html
http://blog.csdn.net/hearrt/article/details/7001358
解决自定义View的onDraw()方法不被执行问题。
当我们需要创建一个直接或间接继承View的类,以便重写onDraw()方法,实现自定义的View的绘制时,往往会发现onDraw方法并没有正确的执行。
你需要在你直接或者間接繼承View的類的構造函數中加入下面的語句:
這個標記在View裡是不設定的。但是像View的一些子類如ViewGroup是可以設定的。典型的,你如果複寫了onDraw(Canvas)方法,你需要清除此標記。
1.要实现一个容器 如此做:
- public class CustomFrameLayout extends FrameLayout {
- private static final String TAG = "CustomFrameLayout";
- public CustomFrameLayout(Context context) {
- super(context);
- this.setWillNotDraw(false);//必须
- }
- public void onDraw(Canvas canvas){
- super.onDraw(canvas);
- drawSomething(canvas);
- }
- }
public class CustomFrameLayout extends FrameLayout {
private static final String TAG = "CustomFrameLayout";
public CustomFrameLayout(Context context) {
super(context);
this.setWillNotDraw(false);//必须
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
drawSomething(canvas);
}
}
构造函数中没有 this.setWillNotDraw(false)这句话时候 onDraw函数始终不被调用 直到添加上之后才可以成功调用onDraw函数
仔细查看onDraw函数的说明:
public void setWillNotDraw (boolean willNotDraw)
If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you overrideonDraw(Canvas)
you should clear this flag.
- 上一篇:svn更新有问题
- 下一篇:Android手机进行无线调试