Skip to content

Instantly share code, notes, and snippets.

@nschwermann
Created December 19, 2014 06:34
Show Gist options
  • Save nschwermann/e566f248723bb1754972 to your computer and use it in GitHub Desktop.
Save nschwermann/e566f248723bb1754972 to your computer and use it in GitHub Desktop.
CircularReveal backport
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.FrameLayout;
public class ClipRevealFrame extends FrameLayout{
private Path mRevealPath;
boolean mClipOutlines;
float mCenterX;
float mCenterY;
float mRadius;
public ClipRevealFrame(Context context) {
super(context);
init();
}
public ClipRevealFrame(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClipRevealFrame(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
mRevealPath = new Path();
mClipOutlines = false;
}
public void setClipOutLines(boolean shouldClip){
mClipOutlines = shouldClip;
}
public void setClipCenter(final int x, final int y){
mCenterX = x;
mCenterY = y;
}
public void setClipRadius(final float radius){
mRadius = radius;
invalidate();
}
@Override
public void draw(Canvas canvas) {
if(!mClipOutlines){
super.draw(canvas);
return;
}
final int state = canvas.save();
mRevealPath.reset();
mRevealPath.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);
canvas.clipPath(mRevealPath);
super.draw(canvas);
canvas.restoreToCount(state);
}
}
private Animator createCheckoutRevealAnimator(final ClipRevealFrame view, int x, int y, float startRadius, float endRadius) {
final Animator reveal;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
reveal = ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius);
} else {
view.setClipOutLines(true);
view.setClipCenter(x, y);
reveal = ObjectAnimator.ofFloat(view, "ClipRadius", startRadius, endRadius);
reveal.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
view.setClipOutLines(false);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
reveal.setDuration(500);
return reveal;
}
}
@nschwermann
Copy link
Author

Thanks to ozodrukh for his work https://github.com/ozodrukh/CircularReveal To me this is a little more simple and I don't care about anything past API 16 so I don't need 9oldandroid.

@Tarubali
Copy link

Is there a sample project?

@nschwermann
Copy link
Author

No but I'll try to put one together.

@defHLT
Copy link

defHLT commented Mar 13, 2015

It doesn't work for me for 4.1-4.3. View just reveals itself at once

@defHLT
Copy link

defHLT commented Mar 14, 2015

It was fun to debug. onDraw was never called. Modifying init to this fixes it:

private void init(){
    mRevealPath = new Path();
    mClipOutlines = false;
    setWillNotDraw(false);
}

P.S. Great gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment