I have created a circular clip path on my canvas and I have a column of numbers that animates on the canvas, so I see numbers animate in the clipped section and animate out. Here is code for clipping:
mClipPath.addCircle((w / 2f), (h / 2f), radius, Path.Direction.CW);
canvas.clipPath(mClipPath, Region.Op.INTERSECT);
As you can see, 0 is animating out and 1 is animating in (the numbers on the right).
But in some cases, the clipping does not happen properly during the animation (3 and 4 on the left).
This is happening only on Nougat.
I'm seeing the same issue. While scrolling a RecyclerView containing the CircleCropDrawable below, I can see the drawable flash between clipped and not clipped depending on the scroll position.
However, through trial and error I saw this only occurred when the Path intersected the edge of the drawable. Reducing the radius by 1dp fixes the issue (Less than 1dp does not work)
CircleCropDrawable.java
package com.someone.android.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.util.TypedValue;
public class CircleCropDrawable extends Drawable {
private final Context mContext;
private final Drawable mDrawable;
public CircleCropDrawable(Context context, Drawable drawable) {
mContext = context;
this.mDrawable = drawable;
}
@Override
public void draw(@NonNull Canvas canvas) {
final float width = getBounds().width();
final float height = getBounds().height();
final Path path = new Path();
// There seems to be a bug in N where the clip path doesn't work when animating
// without a 1dp reduction in radius, the drawable unclips when scrolling in a RecyclerView
float inset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, mContext.getResources().getDisplayMetrics());
path.addCircle(width / 2, height / 2, Math.min(width / 2, height / 2) - inset, Path.Direction.CW);
canvas.clipPath(path);
mDrawable.draw(canvas);
}
@Override
protected void onBoundsChange(Rect bounds) {
mDrawable.setBounds(bounds);
}
@Override
public int getIntrinsicWidth() {
return mDrawable.getIntrinsicWidth();
}
@Override
public int getIntrinsicHeight() {
return mDrawable.getIntrinsicHeight();
}
@Override
public void setAlpha(int i) {
mDrawable.setAlpha(i);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mDrawable.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return mDrawable.getOpacity();
}
}
I had filed a bug with Google on this and it seems to have been resolved. Here is a link to the bug:
https://code.google.com/p/android/issues/detail?id=225016
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With