在dispatchDraw中向ViewGroup添加视图

时间:2013-09-19 07:17:46

标签: android android-canvas android-view viewgroup

我正在尝试创建一个transperancy面具。 我创建了一个扩展ViewGroup的slidingScreen。 在构造函数中我添加:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    LinearLayout roadMap = new LinearLayout(context);
    roadMap.setLayoutParams(params);
    this.addView(roadMap);  

然后我覆盖onDispatchDraw()方法:

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    Log.d("touchy touch", "dispatch draw called");

    canvas.drawColor(getResources().getColor(R.color.LIGHT_BLUE));
    /* recreate the blue rectangle on the canvas */
    path = new Path();
    path.moveTo(0.0f, 0.0f);        // Top Left
    path.lineTo(0.0f, 800.0f);      // Bottom Left
    path.lineTo(0.0f, 800.0f );     // Bottom Right
    path.lineTo(this.x, 0.0f);      // Top Right
    path.close(); 
    canvas.drawPath(path,paint);    
}

结果是我有一个透明的掩码,它等于创建的路径。这不是问题。永远不会显示Unfortunatley roadMap。 我试图覆盖:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int count = this.getChildCount();
    Log.d("children please", "count: "+count);
    for (int i = 0; i < count; i++) {
        View child = this.getChildAt(i);
        child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
        child.invalidate();
        child.bringToFront();
    }

}

然而,我试图连接的线性布局仍未出现。

1 个答案:

答案 0 :(得分:0)

更改为Extend FrameLayout。 删除了这一行:

canvas.drawColor(getResources().getColor(R.color.LIGHT_BLUE));

然后我看到了roadMap视图!