View Animation and Property Animation in Android

  1. View Animation
  2. Property Animation
//Tween animation via xml
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.myanimation);

//Tween animaion through AlphaAnimation object. animation = new AlphaAnimation(1,0); animation.setDuration(2000);
Start Animation:
view.startAnimation(mAnimation);
  1. It only works on a View.
  2. you can only animate limited aspects of a view.
  3. it only changes where the view is drawn on the screen but it does not move the view itself.
  1. ViewAnimator: Simple interface for animating view properties. If Need is to animate just one view and don’t want to repeat it, just use ViewPropertyAnimator
ViewCompat.animate(textView)
.translationX(50f)
.translationY(100f)
.setDuration(1000)
.setInterpolator(AccelerateDecelerateInterpolator())
.setStartDelay(50)
.setListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator) {}
override fun onAnimationEnd(animation: Animator) {}
override fun onAnimationCancel(animation: Animator) {}
override fun onAnimationStart(animation: Animator) {}
})
.withEndAction({ //API 16+
//do something here where animation ends
})
// End Action after animation ends   
Runnable endAction = new Runnable() {
public void run() {
view.animate().x(0);
}
};
view.animate().x(200).withEndAction(endAction);
// start action before animation starts
view.animate().translationX(100).
withStartAction(new Runnable(){
public void run(){
viewer.setTranslationX(100-myView.getWidth());
// do something } });
ObjectAnimator.ofFloat(txt_animate, 
View.ROTATION_X, 0f, 3600f)
.apply {
duration = 5000
interpolator = AccelerateDecelerateInterpolator()//
repeatCount = INFINITE
repeatMode
= REVERSE
start()
}

mAnimator = ObjectAnimator.ofInt(view, "left", 100,
((mSize.x)/2) - (int) getResources().getDimension(R.dimen.dp_35))
.setDuration(2000);
setInterpolator();
mAnimator.start();
}
// With object animator, it call setAlpha automatically when the value is calculated.This makes animating any object much easier, as you no longer need to implement the ValueAnimator.AnimatorUpdateListener, because the animated property updates automatically.
FadeIn Animation:
ObjectAnimator animator = ObjectAnimator.ofFloat(mImageView, "alpha", 1f, 0f);
animator.setDuration(600);
animator.setRepeatCount(1); animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
// With value animator, it does not update the value automatically    // It is suitable for object without a set<PropertyName> method    // The example here is for demonstration only    
ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f); animator.setDuration(600);
animator.setRepeatCount(1); animator.setRepeatMode(ValueAnimator.REVERSE);
// We must explicitly listen to update event and set the value animator.removeAllUpdateListeners(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
float alpha = (float)
animation.getAnimatedValue();
mImageView.setAlpha(alpha);
}
});
animator.start();

--

--

--

Android Developer @Bluejeans by Verizon

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrate Biometric for existing application

Android Studio學習手冊-layout_constraintVertical_bias, layout_constraintHorizontal_bias

Understanding Streams in Flutter (Dart)

Android 10: No dessert for you

Breaking the ice: Service Locator and Dependency Injection Which is What

Testing Dialogs in Robolectric

Navigation with Dynamic Feature Modules

Avoid these mistakes in pubspec.yaml | Tips for Flutter beginners

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Megha Vaishy

Megha Vaishy

Android Developer @Bluejeans by Verizon

More from Medium

Kotlin development — future of android app development

A Look Into Neverland Casino’s Community Engagement

Game Design Conference (aka GDC) Vault📦 is a thing!

YASAN Launcher: 22.01 Update