Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Created August 28, 2018 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guilherme-HRamos/cf6d784e164f1196d2eeff3bf2851a55 to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/cf6d784e164f1196d2eeff3bf2851a55 to your computer and use it in GitHub Desktop.
Medium Pt1 - Material Card Elevation
/** Copyright 2018, Guilherme Henrique Ramos da Silva
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
*/
public class MaterialCardTranslation extends MaterialCardView {
private boolean isElevated = false;
private int ANIM_DURATION = 250;
public MaterialCardTranslation(Context context) {
super(context);
}
public MaterialCardTranslation(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MaterialCardTranslation(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public boolean isElevated() {
return isElevated;
}
public void setDuration(final int duration) {
ANIM_DURATION = duration;
}
public void elevateCard(final boolean elevate, @Nullable final EndAction endAction) {
dispatchElevateAnim(elevate, endAction);
}
public void simulateClickElevation(@Nullable final EndAction endAction) {
dispatchElevateCompleteAnim(endAction);
}
private void dispatchElevateAnim(final boolean elevate, @Nullable final EndAction endAction) {
if (elevate) {
upAnim();
} else {
downAnim();
}
isElevated = elevate;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
endAction.onEnd();
}
}, ANIM_DURATION);
}
private void dispatchElevateCompleteAnim(@Nullable final EndAction endAction) {
if (isElevated || isLessThan21())
return;
final float posZ = getZ();
animate().translationZ(posZ * 2).setDuration(ANIM_DURATION)
.setInterpolator(new FastOutSlowInInterpolator())
.withEndAction(new Runnable() {
@Override
public void run() {
downAnim();
}
});
if (endAction != null)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
endAction.onEnd();
}
}, ANIM_DURATION * 2);
}
private void upAnim() {
if (isLessThan21())
return;
animate().translationZ(getZ() * 2).setDuration(ANIM_DURATION)
.setInterpolator(new FastOutSlowInInterpolator())
.start();
}
private void downAnim() {
if (isLessThan21())
return;
animate().translationZ(0).setDuration(ANIM_DURATION)
.setInterpolator(new FastOutSlowInInterpolator())
.start();
}
public interface EndAction {
void onEnd();
}
private boolean isLessThan21() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment