先来看两个类:
1,Outline
Defines a simple shape, used for bounding graphical regions.
定义一个简单的图形,用定义图形区域的边界
Can be computed for a View, or computed by a Drawable, to drive the shape of shadows cast by a View, or to clip the contents of the View.
可以为一个View计算,或者被一个Drawable计算,驱动一个View的阴影形状的转换(翻译的不好,有更好的翻译将更改)
2,ViewOutlineProvider
Interface by which a View builds its [Outline]
used for shadow casting and clipping.
用于阴影的转换和剪裁
该类是一个接口,只有一个抽象方法
public abstract void getOutline (View view, Outline outline)
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 invalidateOutline() is called explicitly. The input outline is empty and has an alpha of 1.0f
大致翻译下:
得到provider去填充outline时被调用,当View自己的Drawables无效时,View的大小改变,或者invalidateOutline()方法被显示调用的时候这个方法将被调用。输入的outline为空,alpha的值尾1(最后一句怪怪得)
接下来实现一个圆角矩形的ViewOutlineProvider
1,首先实现ViewOutlineProvider并复写其中的抽象方法getOutline
final int margin = Math.min(view.getWidth(), view.getHeight()) / 10;
outline.setRoundRect(margin, margin, view.getWidth() - margin, view.getHeight() - margin, margin / 2);
2,生成实现了ViewOutlineProvider类的对象,
ClipOutlineProvider mOutlineProvider = new ClipOutlineProvider();
//clippedView是要执行操作的View
clippedView.setOutlineProvider(mOutlineProvider);
3,调用相应View的setClipToOutline(boolean b)来设置Outline可用是否可用(true尾可用)