Skip to content

Commit 31d0e9a

Browse files
ymarianikim24
authored andcommitted
[MaterialCardView] Add (set|get)Progress
This allows to animate the shape of the card. Using setProgress so that this works with MotionLayout as well. PiperOrigin-RevId: 257241960
1 parent ce9b98b commit 31d0e9a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/java/com/google/android/material/card/MaterialCardView.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import androidx.annotation.ColorRes;
3232
import androidx.annotation.Dimension;
3333
import androidx.annotation.DrawableRes;
34+
import androidx.annotation.FloatRange;
3435
import androidx.annotation.NonNull;
3536
import androidx.annotation.Nullable;
37+
import com.google.android.material.shape.MaterialShapeDrawable;
3638
import com.google.android.material.shape.MaterialShapeUtils;
3739
import com.google.android.material.shape.ShapeAppearanceModel;
3840
import com.google.android.material.shape.Shapeable;
@@ -239,6 +241,27 @@ float getCardViewRadius() {
239241
return MaterialCardView.super.getRadius();
240242
}
241243

244+
245+
/**
246+
* Sets the interpolation on the Shape Path of the card. Useful for animations.
247+
* @see MaterialShapeDrawable#setInterpolation(float)
248+
* @see ShapeAppearanceModel
249+
*/
250+
public void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
251+
cardViewHelper.setProgress(progress);
252+
}
253+
254+
255+
/**
256+
* Returns the interpolation on the Shape Path of the card.
257+
* @see MaterialShapeDrawable#getInterpolation()
258+
* @see ShapeAppearanceModel
259+
*/
260+
@FloatRange(from = 0f, to = 1f)
261+
public float getProgress() {
262+
return cardViewHelper.getProgress();
263+
}
264+
242265
@Override
243266
public void setContentPadding(int left, int top, int right, int bottom) {
244267
cardViewHelper.setUserContentPadding(left, top, right, bottom);

lib/java/com/google/android/material/card/MaterialCardViewHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.os.Build.VERSION_CODES;
3838
import androidx.annotation.ColorInt;
3939
import androidx.annotation.Dimension;
40+
import androidx.annotation.FloatRange;
4041
import androidx.annotation.NonNull;
4142
import androidx.annotation.Nullable;
4243
import androidx.annotation.RequiresApi;
@@ -288,6 +289,22 @@ float getCornerRadius() {
288289
return shapeAppearanceModel.getTopLeftCorner().getCornerSize();
289290
}
290291

292+
void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
293+
bgDrawable.setInterpolation(progress);
294+
if (foregroundContentDrawable != null) {
295+
foregroundContentDrawable.setInterpolation(progress);
296+
}
297+
298+
if (foregroundShapeDrawable != null) {
299+
foregroundShapeDrawable.setInterpolation(progress);
300+
}
301+
}
302+
303+
@FloatRange(from = 0f, to = 1f)
304+
float getProgress() {
305+
return bgDrawable.getInterpolation();
306+
}
307+
291308
void updateElevation() {
292309
bgDrawable.setElevation(materialCardView.getCardElevation());
293310
}

0 commit comments

Comments
 (0)