Last active
June 4, 2021 21:31
-
-
Save lenamuit/74f02428e1f1fda25b98 to your computer and use it in GitHub Desktop.
Animation builder factory for Android Viewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.annotation.AnimRes; | |
import android.view.View; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
/** | |
* HOW TO USE | |
* AnimationBuilder.from(getActivity()) | |
.setAnimRes(R.anim.rotate2) | |
.delay(1000) | |
.start(viewer); | |
* Created by namlh on 15/04/2015. | |
*/ | |
public class AnimationBuilder { | |
AnimationBuilder(){ | |
} | |
public static AnimationCreator from(Context context){ | |
return new AnimationCreator(context); | |
} | |
public static class AnimationCreator{ | |
private final Context context; | |
private int animRes; | |
private OnAnimationFinishedListener onFinishedListener; | |
private int delay =0; | |
private boolean isForceStart = false; | |
AnimationCreator(Context context){ | |
this.context=context; | |
} | |
public AnimationCreator setAnimRes(@AnimRes int animRes){ | |
this.animRes = animRes; | |
return this; | |
} | |
public AnimationCreator onFinished(OnAnimationFinishedListener listener){ | |
this.onFinishedListener = listener; | |
return this; | |
} | |
public AnimationCreator delay(int milisecond){ | |
this.delay = milisecond; | |
return this; | |
} | |
public AnimationCreator forceStart(boolean isForce){ | |
this.isForceStart = isForce; | |
return this; | |
} | |
public void start(final View view){ | |
view.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
Animation currentAnim = view.getAnimation(); | |
if (currentAnim !=null && !currentAnim.hasEnded()){ | |
if (isForceStart){ | |
currentAnim.cancel(); | |
} | |
else { | |
return; | |
} | |
} | |
Animation animation = AnimationUtils.loadAnimation(context, animRes); | |
animation.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
if (onFinishedListener != null) onFinishedListener.onFinished(view); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
view.startAnimation(animation); | |
} | |
}, delay); | |
} | |
} | |
public interface OnAnimationFinishedListener { | |
void onFinished(View v); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment