使用剪切来圆angularViewGroup的angular落

我有一个RelativeLayout需要有左上angular和右上angular的圆angular。 我可以使用带有topLeftRadius和topRightRadiusangular的XML定义可绘制背景。 但是…这个RelativeLayout还需要有一个背景,它是一个带有平铺位图和形状组合的图层列表,而平铺位图在可绘制XML中没有angular参数。 所以我的想法是用下面的代码做一个RelativeLayout

 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); path.reset(); rect.set(0, 0, w, h); path.addRoundRect(rect, radius, radius, Path.Direction.CW); path.close(); } @Override protected void dispatchDraw(Canvas canvas) { int save = canvas.save(); canvas.clipPath(path); super.dispatchDraw(canvas); canvas.restoreToCount(save); } 

可悲的是没有裁剪发生,我期待它剪辑我的RelativeLayout的所有四个angular落,但没有发生。 “onSizeChanged”和“dispatchDraw”方法都被调用,我testing了。 我也试图closures硬件加速,但它什么都不做。

我的RelativeLayout是一个较大的布局的一部分,这个布局是在FrameLayout的子类中膨胀的,然后这个子类在一个RecyclerView被使用了一行,如果这改变了什么的话。

Solutions Collecting From Web of "使用剪切来圆angularViewGroup的angular落"

定义了这个布局:

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <com.playground.RoundedRelativeLayout android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:background="@color/colorPrimary" /> </FrameLayout> 

RoundedRelativeLayout具有以下实现:

public class RoundedRelativeLayout extends RelativeLayout { private RectF rectF; private Path path = new Path(); private float cornerRadius = 15; public RoundedRelativeLayout(Context context) { super(context); } public RoundedRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public RoundedRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); rectF = new RectF(0, 0, w, h); resetPath(); } @Override public void draw(Canvas canvas) { int save = canvas.save(); canvas.clipPath(path); super.draw(canvas); canvas.restoreToCount(save); } @Override protected void dispatchDraw(Canvas canvas) { int save = canvas.save(); canvas.clipPath(path); super.dispatchDraw(canvas); canvas.restoreToCount(save); } private void resetPath() { path.reset(); path.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW); path.close(); } }
public class RoundedRelativeLayout extends RelativeLayout { private RectF rectF; private Path path = new Path(); private float cornerRadius = 15; public RoundedRelativeLayout(Context context) { super(context); } public RoundedRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public RoundedRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); rectF = new RectF(0, 0, w, h); resetPath(); } @Override public void draw(Canvas canvas) { int save = canvas.save(); canvas.clipPath(path); super.draw(canvas); canvas.restoreToCount(save); } @Override protected void dispatchDraw(Canvas canvas) { int save = canvas.save(); canvas.clipPath(path); super.dispatchDraw(canvas); canvas.restoreToCount(save); } private void resetPath() { path.reset(); path.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW); path.close(); } } 

你会得到以下输出:

RoundKornerLayouts项目的实现被无耻地窃取了。