RelativeLayout圓角處理

RelativeLayout圓角處理以后,可以變相對子view進(jìn)行圓角處理,如ImageView,VideoView等

image

RoundRelativeLayout具體實現(xiàn)

比較簡單,只需要在初始化時設(shè)置一下layout的ViewOutlineProvider,方便起見,這里寫死radius為20dp


/**

* 一個可以把RelativeLayout裁剪成圓角的自定義RelativeLayout布局

* 由于layout里面的子view同樣會被裁剪,例如VideoView,ImageView等,很實用

*

* created by luninggithub

*/

public class RoundRelativeLayout extends RelativeLayout {

    private static final int DEFAULT_ROUND_SIZE = 20;

    public RoundRelativeLayout(Context context) {

        this(context, null);

    }

    public RoundRelativeLayout(Context context, AttributeSet attrs) {

        this(context, attrs, 0);

    }

    public RoundRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {

        this(context, attrs, defStyleAttr, 0);

    }

    public RoundRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

        super(context, attrs, defStyleAttr, defStyleRes);

        init();

    }

    /**

    * 初始化

    */

    private void init() {

        RoundViewOutlineProvider outlineProvider = new RoundViewOutlineProvider(ScreenUtil.dp2px(DEFAULT_ROUND_SIZE));

        setOutlineProvider(outlineProvider);

        setClipToOutline(true);

    }

    /**

    * 圓角ViewOutlineProvider

    */

    private static class RoundViewOutlineProvider extends ViewOutlineProvider {

        private final int roundSize;

        public RoundViewOutlineProvider(int roundSize) {

            this.roundSize = roundSize;

        }

        @Override

        public void getOutline(View view, Outline outline) {

            outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), roundSize);

        }

    }

}

關(guān)鍵類ViewOutlineProvider

ViewOutlineProvider是一個抽象類,系統(tǒng)預(yù)定義了三種不同的outlineprovider,我們只需要實現(xiàn)getOutline方法,在方法內(nèi)部設(shè)置自定義的outline即可。


package android.view;

import android.graphics.Outline;

import android.graphics.drawable.Drawable;

/**

* Interface by which a View builds its {@link Outline}, used for shadow casting and clipping.

*/

public abstract class ViewOutlineProvider {

    // 省略系統(tǒng)預(yù)定義的ViewOutlineProvider代碼...

    /**

    * Called to get the provider to populate the Outline.

    *

    * This method will be called by a View when its owned Drawables are invalidated, when the

    * View's size changes, or if {@link View#invalidateOutline()} is called

    * explicitly.

    *

    * The input outline is empty and has an alpha of <code>1.0f</code>.

    *

    * @param view The view building the outline.

    * @param outline The empty outline to be populated.

    */

    public abstract void getOutline(View view, Outline outline);

}

由注釋可知,getOutline會在view主動調(diào)invalidateOutline()方法是被調(diào)用

RoundRelativeLayout在初始化int是,會設(shè)置自定義的outlineprovider:setOutlineProvider(outlineProvider);

有系統(tǒng)代碼可知,setOutlineProvider方法會主動調(diào)用,如下:


// 省略View的其他代碼......

/**

* Sets the {@link ViewOutlineProvider} of the view, which generates the Outline that defines

* the shape of the shadow it casts, and enables outline clipping.

* <p>

* The default ViewOutlineProvider, {@link ViewOutlineProvider#BACKGROUND}, queries the Outline

* from the View's background drawable, via {@link Drawable#getOutline(Outline)}. Changing the

* outline provider with this method allows this behavior to be overridden.

* <p>

* If the ViewOutlineProvider is null, if querying it for an outline returns false,

* or if the produced Outline is {@link Outline#isEmpty()}, shadows will not be cast.

* <p>

* Only outlines that return true from {@link Outline#canClip()} may be used for clipping.

*

* @see #setClipToOutline(boolean)

* @see #getClipToOutline()

* @see #getOutlineProvider()

*/

public void setOutlineProvider(ViewOutlineProvider provider) {

    mOutlineProvider = provider;

    invalidateOutline(); // 這里會主動調(diào)用自定義的getOutline方法

}

// 省略View的其他代碼......

/**

* Called to rebuild this View's Outline from its {@link ViewOutlineProvider outline provider}

*

* @see #setOutlineProvider(ViewOutlineProvider)

*/

public void invalidateOutline() {

    rebuildOutline();// 見下面方法

    notifySubtreeAccessibilityStateChangedIfNeeded();

    invalidateViewProperty(false, false);

}

// 省略View其他代碼......

private void rebuildOutline() {

    // Unattached views ignore this signal, and outline is recomputed in onAttachedToWindow()

    if (mAttachInfo == null) return;

    if (mOutlineProvider == null) {

        // no provider, remove outline

        mRenderNode.setOutline(null);

    } else {

        final Outline outline = mAttachInfo.mTmpOutline;

        outline.setEmpty();

        outline.setAlpha(1.0f);

        mOutlineProvider.getOutline(this, outline); // 這里調(diào)用

        mRenderNode.setOutline(outline); // 見下面方法

    }

}

/**

* Sets the outline, defining the shape that casts a shadow, and the path to

* be clipped if setClipToOutline is set.

*

* This will make a copy of the provided {@link Outline}, so any future modifications

* to the outline will need to call {@link #setOutline(Outline)} with the modified

* outline for those changes to be applied.

*

* @param outline The outline to use for this RenderNode.

* @return True if the value changed, false if the new value was the same as the previous value.

*/

public boolean setOutline(@Nullable Outline outline) {

    if (outline == null) {

        return nSetOutlineNone(mNativeRenderNode);

    }

    switch (outline.mMode) {

        case Outline.MODE_EMPTY:

            return nSetOutlineEmpty(mNativeRenderNode);

        case Outline.MODE_ROUND_RECT: // 走到這里

            return nSetOutlineRoundRect(mNativeRenderNode,

                    outline.mRect.left, outline.mRect.top,

                    outline.mRect.right, outline.mRect.bottom,

                    outline.mRadius, outline.mAlpha);

        case Outline.MODE_PATH:

            return nSetOutlinePath(mNativeRenderNode, outline.mPath.mNativePath,

                    outline.mAlpha);

    }

    throw new IllegalArgumentException("Unrecognized outline?");

}

// 省略View代碼......

public final class RenderNode {

// 省略無關(guān)代碼...

@CriticalNative

private static native boolean nSetOutlineRoundRect(long renderNode, int left, int top,

        int right, int bottom, float radius, float alpha);

}

這樣一看就比較清晰了

init -→ setOutlineProvider -→ invalidateOutline -→ rebuildOutline -→ setOutline -→ nSetOutlineRoundRect(Native接口)
Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容