Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Nougat clipPath not working during animation

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). enter image description here

But in some cases, the clipping does not happen properly during the animation (3 and 4 on the left).

enter image description here

This is happening only on Nougat.

like image 916
Santoshastagi Avatar asked Sep 30 '16 19:09

Santoshastagi


2 Answers

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();
    }
}
like image 74
nathanielwolf Avatar answered Oct 12 '22 23:10

nathanielwolf


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

like image 43
Santoshastagi Avatar answered Oct 12 '22 23:10

Santoshastagi