我有一个自定义ViewGroup
,我将其覆盖dispatchDraw
并绘制一些额外的Path
,如下所示:
@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
之上?{/ p>
提前致谢!
答案 0 :(得分:2)
为了吸引额外Path
以上的孩子,您应在绘制super.dispatchDraw(canvas)
后致电Path
。
@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);
}
答案 1 :(得分: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>