n4zArh n4zArh - 5 months ago 102
Android Question

Antialias on clipPath on layout

I have a layout, which has several views inside of it - toolbar, recyclerview and few separators (which are simple views with height of 2dp and match_parent width). I wanted to put a mask on a layout - most important part of it are the round corners for whole layout (and not the views itself). I decided to create my own LinearLayout class with overloaded dispatchDraw function and I managed to get nice result... except for one thing - those corners are not antialiased.

tl;dr is there ANY way to put antialias to clipPath function? I know can turn it on in Paint and then use xfermodes to mask the layout, however I have no clue how to draw mask and then draw everything else (without knowing what's exactly inside).

Here is my layout code (except for classname, simple constructors and Path field):

@Override protected void dispatchDraw(Canvas canvas) {
if (path == null) {
path = new Path();
float margin = MyApplication.getInstance().getMetrics().density * 5;
path.addRoundRect(new RectF(margin,margin,getWidth()-margin, getHeight()-margin),
margin*2, margin*2, Path.Direction.CW);
}
canvas.clipPath(path);
super.dispatchDraw(canvas);
}

@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}

Answer

try this SO answer, of course instead of mMask.draw(canvas) which is a "mask" made by a NinePatchDrawable you would need to call canvas.drawPath() with a Paint set with PorterDuff.Mode.DST_IN xfer mode

Comments