Merge "Fix invalid ripple bounds" into sc-dev

This commit is contained in:
Jay Aliomer
2021-06-18 03:31:35 +00:00
committed by Android (Google) Code Review
2 changed files with 49 additions and 44 deletions

View File

@@ -60,6 +60,10 @@ public final class RippleAnimationSession {
mForceSoftware = forceSoftware; mForceSoftware = forceSoftware;
} }
boolean isForceSoftware() {
return mForceSoftware;
}
@NonNull RippleAnimationSession enter(Canvas canvas) { @NonNull RippleAnimationSession enter(Canvas canvas) {
mStartTime = AnimationUtils.currentAnimationTimeMillis(); mStartTime = AnimationUtils.currentAnimationTimeMillis();
if (isHwAccelerated(canvas)) { if (isHwAccelerated(canvas)) {
@@ -130,7 +134,6 @@ public final class RippleAnimationSession {
return this; return this;
} }
private void exitHardware(RecordingCanvas canvas) { private void exitHardware(RecordingCanvas canvas) {
AnimationProperties<CanvasProperty<Float>, CanvasProperty<Paint>> AnimationProperties<CanvasProperty<Float>, CanvasProperty<Paint>>
props = getCanvasProperties(); props = getCanvasProperties();
@@ -199,6 +202,15 @@ public final class RippleAnimationSession {
startAnimation(expand, loop); 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() { @NonNull AnimationProperties<Float, Paint> getProperties() {
return mProperties; return mProperties;
} }
@@ -249,7 +261,7 @@ public final class RippleAnimationSession {
static class AnimationProperties<FloatType, PaintType> { static class AnimationProperties<FloatType, PaintType> {
private final FloatType mProgress; private final FloatType mProgress;
private final FloatType mMaxRadius; private FloatType mMaxRadius;
private final FloatType mNoisePhase; private final FloatType mNoisePhase;
private final PaintType mPaint; private final PaintType mPaint;
private final RippleShader mShader; private final RippleShader mShader;
@@ -273,6 +285,10 @@ public final class RippleAnimationSession {
return mProgress; return mProgress;
} }
void setRadius(FloatType radius) {
mMaxRadius = radius;
}
void setOrigin(FloatType x, FloatType y) { void setOrigin(FloatType x, FloatType y) {
mX = x; mX = x;
mY = y; 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() { private void onHotspotBoundsChanged() {
final int count = mExitingRipplesCount; final int count = mExitingRipplesCount;
@@ -750,6 +750,20 @@ public class RippleDrawable extends LayerDrawable {
if (mBackground != null) { if (mBackground != null) {
mBackground.onHotspotBoundsChanged(); 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) { private void drawPatterned(@NonNull Canvas canvas) {
final Rect bounds = getDirtyBounds(); final Rect bounds = mHotspotBounds;
final int saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG); final int saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
boolean useCanvasProps = shouldUseCanvasProps(canvas); boolean useCanvasProps = shouldUseCanvasProps(canvas);
boolean changedHotspotBounds = !bounds.equals(mHotspotBounds);
if (isBounded()) { if (isBounded()) {
canvas.clipRect(bounds); canvas.clipRect(getDirtyBounds());
} }
final float x, y, cx, cy, w, h; final float x, y, cx, cy, w, h;
if (changedHotspotBounds) { boolean addRipple = mAddRipple;
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(); cx = bounds.centerX();
cy = bounds.centerY(); cy = bounds.centerY();
h = bounds.height();
w = bounds.width();
}
boolean addRipple = mAddRipple;
boolean shouldExit = mExitingAnimation; boolean shouldExit = mExitingAnimation;
mExitingAnimation = false; mExitingAnimation = false;
mAddRipple = false; mAddRipple = false;
@@ -875,6 +874,16 @@ public class RippleDrawable extends LayerDrawable {
drawContent(canvas); drawContent(canvas);
drawPatternedBackground(canvas, cx, cy); drawPatternedBackground(canvas, cx, cy);
if (addRipple && mRunningAnimations.size() <= MAX_RIPPLES) { 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 = RippleAnimationSession.AnimationProperties<Float, Paint> properties =
createAnimationProperties(x, y, cx, cy, w, h); createAnimationProperties(x, y, cx, cy, w, h);
mRunningAnimations.add(new RippleAnimationSession(properties, !useCanvasProps) mRunningAnimations.add(new RippleAnimationSession(properties, !useCanvasProps)
@@ -898,33 +907,13 @@ public class RippleDrawable extends LayerDrawable {
CanvasProperty<Paint>> CanvasProperty<Paint>>
p = s.getCanvasProperties(); p = s.getCanvasProperties();
RecordingCanvas can = (RecordingCanvas) canvas; RecordingCanvas can = (RecordingCanvas) canvas;
CanvasProperty<Float> xProp, yProp; can.drawRipple(p.getX(), p.getY(), p.getMaxRadius(), p.getPaint(),
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(),
p.getProgress(), p.getNoisePhase(), p.getColor(), p.getShader()); p.getProgress(), p.getNoisePhase(), p.getColor(), p.getShader());
} else { } else {
RippleAnimationSession.AnimationProperties<Float, Paint> p = RippleAnimationSession.AnimationProperties<Float, Paint> p =
s.getProperties(); 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(); float radius = p.getMaxRadius();
canvas.drawCircle(xProp, yProp, radius, p.getPaint()); canvas.drawCircle(p.getX(), p.getY(), radius, p.getPaint());
} }
} }
canvas.restoreToCount(saveCount); canvas.restoreToCount(saveCount);