Skip to content

Commit

Permalink
[MaterialCardView] Add (set|get)Progress
Browse files Browse the repository at this point in the history
This allows to animate the shape of the card.
Using setProgress so that this works with MotionLayout as well.

PiperOrigin-RevId: 257241960
  • Loading branch information
ymarian authored and ikim24 committed Jul 10, 2019
1 parent ce9b98b commit 31d0e9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/java/com/google/android/material/card/MaterialCardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import androidx.annotation.ColorRes;
import androidx.annotation.Dimension;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.MaterialShapeUtils;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.shape.Shapeable;
Expand Down Expand Up @@ -239,6 +241,27 @@ float getCardViewRadius() {
return MaterialCardView.super.getRadius();
}


/**
* Sets the interpolation on the Shape Path of the card. Useful for animations.
* @see MaterialShapeDrawable#setInterpolation(float)
* @see ShapeAppearanceModel
*/
public void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
cardViewHelper.setProgress(progress);
}


/**
* Returns the interpolation on the Shape Path of the card.
* @see MaterialShapeDrawable#getInterpolation()
* @see ShapeAppearanceModel
*/
@FloatRange(from = 0f, to = 1f)
public float getProgress() {
return cardViewHelper.getProgress();
}

@Override
public void setContentPadding(int left, int top, int right, int bottom) {
cardViewHelper.setUserContentPadding(left, top, right, bottom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.os.Build.VERSION_CODES;
import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
Expand Down Expand Up @@ -288,6 +289,22 @@ float getCornerRadius() {
return shapeAppearanceModel.getTopLeftCorner().getCornerSize();
}

void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
bgDrawable.setInterpolation(progress);
if (foregroundContentDrawable != null) {
foregroundContentDrawable.setInterpolation(progress);
}

if (foregroundShapeDrawable != null) {
foregroundShapeDrawable.setInterpolation(progress);
}
}

@FloatRange(from = 0f, to = 1f)
float getProgress() {
return bgDrawable.getInterpolation();
}

void updateElevation() {
bgDrawable.setElevation(materialCardView.getCardElevation());
}
Expand Down

0 comments on commit 31d0e9a

Please sign in to comment.