How to simultaneously clip a ViewGroup's children *and* its background Drawable?

时间:2016-03-02 10:52:07

标签: android android-layout android-canvas android-drawable

I have a custom LinearLayout class which I'm using to clip all of its child Views through Canvas#clipPath():

public class MaskedLinearLayout extends LinearLayout {

    protected Path mMaskingClip = new Path();

    (...)

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // Update clipping path
        mMaskingClip.reset();
        (...)
        mMaskingClip.close();
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        int save = canvas.save();
        canvas.clipPath(mMaskingClip);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(save);
    }
}

The code above works great, but the background Drawable that I've set for the same MaskedLinearLayout is not clipped to the path that I've defined (only its children).

How should I accomplish this? Some things that I've tried include overriding method onDraw() (with similar code shown in method dispatchDraw() above) and setting its background Drawable boundaries manually (Drawable#setBounds()), both without success.

Note that I cannot use static Drawables in this layout, because the latter should be able to be resized/masked dynamically while being displayed on the screen.

Thank you in advance for any answer.

0 个答案:

没有答案