2013-12-11 17 views
0

我有一个自定义ViewGroup的顶部添加孩子在那里我重写它dispatchDraw和绘制一些额外的Path S的看起来是这样的:的Android ViewGroup中的所有图纸

@Override 
public void dispatchDraw(Canvas canvas) { 
    super.dispatchDraw(canvas); 
    canvas.setMatrix(matrix); 
    mRegions.clear(); 
    canvas.drawPicture(mPicture); 
    if (drawShader) { 
     canvas.drawBitmap(mShader, 0, 0, mBitmapPaint); 
    } 
    for (int i = 0; i < mPaths.size(); i++) { 
     if (mIndex != -1 && mIndex == i) { 
      canvas.save(); 
      canvas.drawRect(mRect, mPaint); 
      drawPaths(canvas, mPaths.get(i)); 
      canvas.clipPath(mPaths.get(i), Region.Op.INTERSECT); 
      canvas.drawColor(mOverlayColor); 
      Rect f = mRegions.get(i).getBounds(); 
      mPaint.setColor(Color.RED); 
      mPaint.setStyle(Paint.Style.STROKE); 
      canvas.drawCircle(f.centerX() - mRadius, f.centerY() - mRadius, mRadius, mPaint); 
      canvas.restore(); 
     } else { 
      drawPaths(canvas, mPaths.get(i)); 
     } 
    } 
} 

现在,这是我想要的东西就是加一个孩子将被放置在这些东西上绘制dispatchDraw。现在我这样做:

mBalloon = LayoutInflater.from(getContext()).inflate(R.layout.map_balloon, null); 
    LayoutParams params = new LayoutParams(500, 150); 
    mBalloon.setLayoutParams(params); 
    addView(mBalloon); 
    bringChildToFront(getChildAt(0)); 
init()方法

被称为在我ViewGroup的构造,其结果是,在dispatchDraw画都放在上面mBalloon

任何想法如何将我的View - mBalloon放在dispatchDraw以上的东西上面?

在此先感谢!

+0

您是否尝试在自定义绘图之后调用drawChild? – pskink

回答

1

我明白你想达到的目标的一般想法,我会尝试建议一种不同的方法。

从文档:A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.

所以一个ViewGroup中不应该在屏幕上绘制的东西都没有。有可能做到这一点?是的,但它只是让你的OO结构没有做好。

所以我的建议是使用任何ViewGroup你喜欢FrameLayout,RelativeLayout等。并将路径绘制代码迁移到自定义视图,将绘制屏幕上的所有这些路径,并在标准ViewGroup中,你只需要定位相应的自定义视图。大概是这样的:

<RelativeLayout> 

    <TextView/> // just an example 
    <ImageView/> // just an example 

    <CustomPathView/> // your custom view 

    <FrameLayout/> // in this frame you can put views to be on top 
</RelativeLayout> 
+0

我这样做的事情只是因为我需要根据所选路径更新'mBalloon'的位置,并在我移动ViewGroup中的路径时移动它。 – hardartcore

+0

我明白,你的CustomChild总是有方法可以接收来自CustomViewGroups(不绘制任何东西)与接口等回调。其他选项是所有childViews在'super.dispatchDraw'期间绘制,所以你将不得不检查源代码并自行重新实现。 – Budius

+0

这里是行2876 https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/ViewGroup.java – Budius

2

绘制你的孩子的额外Path上图中Path后,你应该叫super.dispatchDraw(canvas)

@Override 
public void dispatchDraw(Canvas canvas) { 
    canvas.setMatrix(matrix); 
    mRegions.clear(); 
    canvas.drawPicture(mPicture); 
    if (drawShader) { 
     canvas.drawBitmap(mShader, 0, 0, mBitmapPaint); 
    } 
    for (int i = 0; i < mPaths.size(); i++) { 
     if (mIndex != -1 && mIndex == i) { 
      canvas.save(); 
      canvas.drawRect(mRect, mPaint); 
      drawPaths(canvas, mPaths.get(i)); 
      canvas.clipPath(mPaths.get(i), Region.Op.INTERSECT); 
      canvas.drawColor(mOverlayColor); 
      Rect f = mRegions.get(i).getBounds(); 
      mPaint.setColor(Color.RED); 
      mPaint.setStyle(Paint.Style.STROKE); 
      canvas.drawCircle(f.centerX() - mRadius, f.centerY() - mRadius, mRadius, mPaint); 
      canvas.restore(); 
     } else { 
      drawPaths(canvas, mPaths.get(i)); 
     } 
    } 
    super.dispatchDraw(canvas); 
}