Frage

I have a custom ViewGroup where I am overriding it's dispatchDraw and drawing some extra Paths which looks like this :

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

Now the things which I want is to add a child which will be placed above these things drawn on dispatchDraw. For now I am doing this :

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

in my init() method which is called in my ViewGroup's constructor and the result is that drawing in dispatchDraw are placed above mBalloon.

Any idea how can I place my View - mBalloon above the things from dispatchDraw?

Thanks in advance!

War es hilfreich?

Lösung 2

I understand the general idea of what you're trying to achieve and I'll try to suggest a different approach.

from the docs: 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.

So a ViewGroup is not supposed to be drawing stuff on the screen at all. It is possible to do it? Yes, but it just makes your OO structure less properly done.

So my suggestion is to use whatever ViewGroup you prefer FrameLayout, RelativeLayout, etc. And migrate the path draw code to a custom View that will draw all those paths in the screen, and in the standard ViewGroup you just have to position the custom View accordingly. probably something like:

<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>

Andere Tipps

For drawing childs above your extra Path you should call super.dispatchDraw(canvas) after drawing 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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top