Created
March 23, 2015 15:48
-
-
Save fdoyle/878969b3e485c675c034 to your computer and use it in GitHub Desktop.
Reveal animation, bound in framelayout, but works pre 5.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.derp.revealanimationdemo; | |
import android.animation.ObjectAnimator; | |
import android.animation.ValueAnimator; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffXfermode; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.widget.FrameLayout; | |
/** | |
* Created by fdoyle on 3/23/15. | |
*/ | |
public class RevealingFrameLayout extends FrameLayout { | |
Paint paint; | |
public float radius = 0; | |
boolean isRevealed = false; | |
public RevealingFrameLayout(Context context) { | |
super(context); | |
init(); | |
} | |
public RevealingFrameLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public RevealingFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
public RevealingFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
private void init() { | |
setLayerType(LAYER_TYPE_SOFTWARE, null); | |
paint = new Paint(); | |
PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP); | |
paint.setXfermode(mode); | |
paint.setColor(Color.TRANSPARENT); | |
} | |
public boolean isRevealed() { | |
return isRevealed; | |
} | |
public float getRadius() { | |
return radius; | |
} | |
private void setRadius(float radius) { | |
this.radius = radius; | |
invalidate(); | |
} | |
public void setRevealed(boolean revealed) { | |
if (revealed) { | |
Log.d("TAG", "expanding"); | |
isRevealed = true; | |
ObjectAnimator.ofFloat(this, "radius", 1f).setDuration(200).start(); | |
} else { | |
Log.d("TAG", "contracting"); | |
isRevealed = false; | |
ObjectAnimator.ofFloat(this, "radius", 0f).setDuration(200).start(); | |
} | |
} | |
@Override | |
protected void dispatchDraw(Canvas canvas) { | |
int w = canvas.getWidth(); | |
int h = canvas.getHeight(); | |
float hypotenuse = (float) Math.sqrt(w * w + h * h); | |
super.dispatchDraw(canvas); | |
Path rect = new Path(); | |
rect.addRect(0, 0, w, h, Path.Direction.CW); | |
Path circlePath = new Path(); | |
circlePath.addCircle(w * .9f, h * .9f, radius * hypotenuse, Path.Direction.CCW); | |
circlePath.setFillType(Path.FillType.EVEN_ODD); | |
rect.addPath(circlePath); | |
canvas.drawPath(rect, paint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment