Fix invalid ripple bounds

Test: manual
Fixes: 188112758
Change-Id: I6e23c02adc4761fde2e69bafc341066be15f63ee
This commit is contained in:
Lucas Dupin
2021-06-10 17:19:39 -07:00
committed by Jay Aliomer
parent 9da336b4c1
commit 8bdce16ce4
2 changed files with 49 additions and 44 deletions

View File

@@ -60,6 +60,10 @@ public final class RippleAnimationSession {
mForceSoftware = forceSoftware;
}
boolean isForceSoftware() {
return mForceSoftware;
}
@NonNull RippleAnimationSession enter(Canvas canvas) {
mStartTime = AnimationUtils.currentAnimationTimeMillis();
if (isHwAccelerated(canvas)) {
@@ -130,7 +134,6 @@ public final class RippleAnimationSession {
return this;
}
private void exitHardware(RecordingCanvas canvas) {
AnimationProperties<CanvasProperty<Float>, CanvasProperty<Paint>>
props = getCanvasProperties();
@@ -199,6 +202,15 @@ public final class RippleAnimationSession {
startAnimation(expand, loop);
}
void setRadius(float radius) {
mProperties.setRadius(radius);
mProperties.getShader().setRadius(radius);
if (mCanvasProperties != null) {
mCanvasProperties.setRadius(CanvasProperty.createFloat(radius));
mCanvasProperties.getShader().setRadius(radius);
}
}
@NonNull AnimationProperties<Float, Paint> getProperties() {
return mProperties;
}
@@ -249,7 +261,7 @@ public final class RippleAnimationSession {
static class AnimationProperties<FloatType, PaintType> {
private final FloatType mProgress;
private final FloatType mMaxRadius;
private FloatType mMaxRadius;
private final FloatType mNoisePhase;
private final PaintType mPaint;
private final RippleShader mShader;
@@ -273,6 +285,10 @@ public final class RippleAnimationSession {
return mProgress;
}
void setRadius(FloatType radius) {
mMaxRadius = radius;
}
void setOrigin(FloatType x, FloatType y) {
mX = x;
mY = y;

View File

@@ -734,7 +734,7 @@ public class RippleDrawable extends LayerDrawable {
}
/**
* Notifies all the animating ripples that the hotspot bounds have changed.
* Notifies all the animating ripples that the hotspot bounds have changed and modify sessions.
*/
private void onHotspotBoundsChanged() {
final int count = mExitingRipplesCount;
@@ -750,6 +750,20 @@ public class RippleDrawable extends LayerDrawable {
if (mBackground != null) {
mBackground.onHotspotBoundsChanged();
}
float newRadius = Math.round(computeRadius());
for (int i = 0; i < mRunningAnimations.size(); i++) {
RippleAnimationSession s = mRunningAnimations.get(i);
s.setRadius(newRadius);
s.getProperties().getShader()
.setResolution(mHotspotBounds.width(), mHotspotBounds.height());
float cx = mHotspotBounds.centerX(), cy = mHotspotBounds.centerY();
s.getProperties().getShader().setOrigin(cx, cy);
s.getProperties().setOrigin(cx, cy);
if (!s.isForceSoftware()) {
s.getCanvasProperties()
.setOrigin(CanvasProperty.createFloat(cx), CanvasProperty.createFloat(cy));
}
}
}
/**
@@ -840,31 +854,16 @@ public class RippleDrawable extends LayerDrawable {
}
private void drawPatterned(@NonNull Canvas canvas) {
final Rect bounds = getDirtyBounds();
final Rect bounds = mHotspotBounds;
final int saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
boolean useCanvasProps = shouldUseCanvasProps(canvas);
boolean changedHotspotBounds = !bounds.equals(mHotspotBounds);
if (isBounded()) {
canvas.clipRect(bounds);
canvas.clipRect(getDirtyBounds());
}
final float x, y, cx, cy, w, h;
if (changedHotspotBounds) {
x = mHotspotBounds.exactCenterX();
y = mHotspotBounds.exactCenterY();
cx = x;
cy = y;
h = mHotspotBounds.height();
w = mHotspotBounds.width();
useCanvasProps = false;
} else {
x = mPendingX;
y = mPendingY;
cx = bounds.centerX();
cy = bounds.centerY();
h = bounds.height();
w = bounds.width();
}
boolean addRipple = mAddRipple;
cx = bounds.centerX();
cy = bounds.centerY();
boolean shouldExit = mExitingAnimation;
mExitingAnimation = false;
mAddRipple = false;
@@ -875,6 +874,16 @@ public class RippleDrawable extends LayerDrawable {
drawContent(canvas);
drawPatternedBackground(canvas, cx, cy);
if (addRipple && mRunningAnimations.size() <= MAX_RIPPLES) {
if (mHasPending) {
x = mPendingX;
y = mPendingY;
mHasPending = false;
} else {
x = bounds.exactCenterX();
y = bounds.exactCenterY();
}
h = bounds.height();
w = bounds.width();
RippleAnimationSession.AnimationProperties<Float, Paint> properties =
createAnimationProperties(x, y, cx, cy, w, h);
mRunningAnimations.add(new RippleAnimationSession(properties, !useCanvasProps)
@@ -898,33 +907,13 @@ public class RippleDrawable extends LayerDrawable {
CanvasProperty<Paint>>
p = s.getCanvasProperties();
RecordingCanvas can = (RecordingCanvas) canvas;
CanvasProperty<Float> xProp, yProp;
if (changedHotspotBounds) {
xProp = CanvasProperty.createFloat(x);
yProp = CanvasProperty.createFloat(y);
p.getShader().setTouch(x, y);
p.getShader().setOrigin(x, y);
} else {
xProp = p.getX();
yProp = p.getY();
}
can.drawRipple(xProp, yProp, p.getMaxRadius(), p.getPaint(),
can.drawRipple(p.getX(), p.getY(), p.getMaxRadius(), p.getPaint(),
p.getProgress(), p.getNoisePhase(), p.getColor(), p.getShader());
} else {
RippleAnimationSession.AnimationProperties<Float, Paint> p =
s.getProperties();
float xProp, yProp;
if (changedHotspotBounds) {
xProp = x;
yProp = y;
p.getShader().setTouch(x, y);
p.getShader().setOrigin(x, y);
} else {
xProp = p.getX();
yProp = p.getY();
}
float radius = p.getMaxRadius();
canvas.drawCircle(xProp, yProp, radius, p.getPaint());
canvas.drawCircle(p.getX(), p.getY(), radius, p.getPaint());
}
}
canvas.restoreToCount(saveCount);