Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
Created August 11, 2020 11:05
Show Gist options
  • Save yuriyskulskiy/07818f681727dcf4688f058c4a9edc9a to your computer and use it in GitHub Desktop.
Save yuriyskulskiy/07818f681727dcf4688f058c4a9edc9a to your computer and use it in GitHub Desktop.
Distort edge effect: custom circular edge equation
public class DistortEdgeEffect {
...
private int mFulHeight;
private int mFullWidth;
private float maxDistortionHeight;
...
public void setSize(int width, int height) {
...
mFulHeight = height;
mFullWidth = width;
maxDistortionHeight = h;
}
...
public boolean drawWithDistortion(Canvas canvas) {
update();
float currentRadiusMultiplier = Math.min(mGlowScaleY, 1.f) * mBaseGlowScale;
float currentDistortionHeight = currentRadiusMultiplier * maxDistortionHeight;
final float centerX = mDisplacement * mFullWidth;
final float centerY = computeCenterY(currentDistortionHeight);
float rLength = (currentDistortionHeight + Math.abs(centerY));
canvas.drawCircle(centerX, centerY, rLength, mPaint);
boolean oneLastFrame = false;
if (mState == STATE_RECEDE && mGlowScaleY == 0) {
mState = STATE_IDLE;
oneLastFrame = true;
}
return mState != STATE_IDLE || oneLastFrame;
}
private float computeCenterY(float distortionHeight) {
float leftDisplacementOffset = mDisplacement * mFullWidth;
float x1;
float y1 = 0;
float x2 = leftDisplacementOffset;
float y2 = distortionHeight;
float x3;
float y3 = 0;
if (mDisplacement <= 0.5) {
x1 = 2 * leftDisplacementOffset - mFullWidth;
x3 = mFullWidth;
} else {
x1 = 0;
x3 = 2 * leftDisplacementOffset;
}
double numerator = (double) (x1 * (Math.pow(x2, 2) + Math.pow(y2, 2)
- Math.pow(x3, 2) - Math.pow(y3, 2))
+ x2 * (Math.pow(x3, 2) + Math.pow(y3, 2)
- Math.pow(x1, 2) - Math.pow(y1, 2))
+ x3 * (Math.pow(x1, 2) + Math.pow(y1, 2)
- Math.pow(x2, 2) - Math.pow(y2, 2)));
float denominator = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
return (float) (numerator / denominator);
}
...
}
public class NestedScrollView extends FrameLayout
implements NestedScrollingParent3,
NestedScrollingChild3, ScrollingView {
...
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
...
mEdgeGlowTop.setSize(width, height);
// if (mEdgeGlowTop.draw(canvas)) {
if (mEdgeGlowTop.drawWithDistortion(canvas)) {
ViewCompat.postInvalidateOnAnimation(this);
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment