现在的位置: 首页 > 综合 > 正文

View的onDraw函数不被调用

2013年09月14日 ⁄ 综合 ⁄ 共 889字 ⁄ 字号 评论关闭

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);
		}
	}

构造函数中没有this.setWillNotDraw(false)这句话时候onDraw函数始终不被调用
直到添加上之后才可以成功调用onDraw函数

仔细查看onDraw函数的说明:

public void setWillNotDraw (boolean willNotDraw)

Since: API Level 1

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.

Parameters
willNotDraw whether or not this View draw on its own
总之,要想再自定义的画一些东西 就要进行setWillNotDraw(false).
2.如果你有两个容器 A 和 a,a是放在A之中。如果都对这两个容器设置了setWillNotDraw(false),那么你将会看到一个“奇迹":a中被添加进来的控件都不透明了,这时候不要怀疑你的控件图片不是透明的,是因为你对A容器设置了setWillNotDraw(false),尝试将其去掉,问题就OK了

抱歉!评论已关闭.