Ini hanyalah salah satu trik agar proses remove view pada LinearLayout tampak lebih menarik dimata user dan tidak kaku.
Saya membuat satu kelas untuk menangani hal ini yaitu DeleteRowAnimation turunan dari kelas Animation.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
package id.web.hangga.deleterowanim; import android.os.Handler; import android.view.View; import android.view.animation.Animation; import android.view.animation.Transformation; import android.widget.LinearLayout; /** * Created by hangga on 21/12/16. */ public class DeleteRowAnimation extends Animation { public static int ALPHA = 0; public static int SWIPE_LEFT = 1; public static int SWIPE_UP = 2; public void setAnimType(int animType) { this.animType = animType; } private int animType = ALPHA; // default final int startWidth; final int targetWidth; final int startHeight; final int targetHeight; final float startAlpha; final float targetAlpha; private DeleteRowAnimation deleteRowAnimation; private View view; public DeleteRowAnimation(final View view) { deleteRowAnimation = this; this.view = view; this.targetHeight = 0; this.startHeight = view.getHeight(); this.targetWidth = 0; this.startWidth = view.getWidth(); this.startAlpha = view.getAlpha(); this.targetAlpha = 0; this.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { new Handler().postDelayed(new Runnable() { @Override public void run() { ((LinearLayout)view.getParent()).removeView(view); } }, deleteRowAnimation.getDuration()); } @Override public void onAnimationRepeat(Animation animation) { } }); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { if (this.animType == ALPHA) { float newAlpha = (startAlpha + (targetAlpha - startAlpha) * interpolatedTime); view.setAlpha(newAlpha); } else if (this.animType == SWIPE_LEFT){ int newWidth = (int) (startWidth + (targetWidth - startWidth) * interpolatedTime); view.getLayoutParams().width = newWidth; view.requestLayout(); } else if (this.animType == SWIPE_UP){ int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime); view.getLayoutParams().height = newHeight; view.requestLayout(); } } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); } @Override public boolean willChangeBounds() { return true; } @Override public boolean hasStarted() { return super.hasStarted(); } } |
Contoh implementasinya adalah sebagai berikut :
Alpha
0 1 2 3 |
DeleteRowAnimation anim = new DeleteRowAnimation(child); anim.setAnimType(DeleteRowAnimation.ALPHA); anim.setDuration(1000); child.startAnimation(anim); |
Swipe Left
0 1 2 3 |
DeleteRowAnimation anim = new DeleteRowAnimation(child); anim.setAnimType(DeleteRowAnimation.SWIPE_LEFT); anim.setDuration(1000); child.startAnimation(anim); |
Swipe Up
0 1 2 3 |
DeleteRowAnimation anim = new DeleteRowAnimation(child); anim.setAnimType(DeleteRowAnimation.SWIPE_UP); anim.setDuration(1000); child.startAnimation(anim); |