Created
December 29, 2017 10:45
-
-
Save vipulshah2010/9d33ccc07897758db01b7d7fac2e692a to your computer and use it in GitHub Desktop.
Object Animators in Android
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
| package vipshah.com.objectanimations; | |
| import android.animation.AnimatorSet; | |
| import android.animation.ObjectAnimator; | |
| import android.animation.ValueAnimator; | |
| import android.os.Bundle; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.view.View; | |
| import android.widget.Button; | |
| public class MainActivity extends AppCompatActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| final Button alphaButton = findViewById(R.id.alphaButton); | |
| final Button rotateButton = findViewById(R.id.rotateButton); | |
| final Button translateButton = findViewById(R.id.translateButton); | |
| final Button scaleButton = findViewById(R.id.scaleButton); | |
| alphaButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| playAlphaAnimation(alphaButton); | |
| } | |
| }); | |
| rotateButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| playRotateAnimation(rotateButton); | |
| } | |
| }); | |
| translateButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| playTranslateAnimation(translateButton); | |
| } | |
| }); | |
| scaleButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| playScaleAnimation(scaleButton); | |
| } | |
| }); | |
| } | |
| private void playAlphaAnimation(Button alphaButton) { | |
| ObjectAnimator animator = ObjectAnimator | |
| .ofFloat(alphaButton, View.ALPHA, 1, 0); | |
| animator.setDuration(700); | |
| animator.setRepeatCount(ValueAnimator.INFINITE); | |
| animator.setRepeatMode(ValueAnimator.REVERSE); | |
| animator.start(); | |
| } | |
| private void playRotateAnimation(Button rotateButton) { | |
| ObjectAnimator animator = ObjectAnimator | |
| .ofFloat(rotateButton, View.ROTATION, 0, 270); | |
| animator.setDuration(700); | |
| animator.setRepeatCount(ValueAnimator.INFINITE); | |
| animator.setRepeatMode(ValueAnimator.REVERSE); | |
| animator.start(); | |
| } | |
| private void playTranslateAnimation(Button translateButton) { | |
| ObjectAnimator xAnimator = ObjectAnimator | |
| .ofFloat(translateButton, View.TRANSLATION_X, 1, 200); | |
| xAnimator.setRepeatCount(ValueAnimator.INFINITE); | |
| xAnimator.setRepeatMode(ValueAnimator.REVERSE); | |
| ObjectAnimator yAnimator = ObjectAnimator | |
| .ofFloat(translateButton, View.TRANSLATION_Y, 1, 200); | |
| yAnimator.setRepeatCount(ValueAnimator.INFINITE); | |
| yAnimator.setRepeatMode(ValueAnimator.REVERSE); | |
| AnimatorSet set = new AnimatorSet(); | |
| set.playTogether(xAnimator, yAnimator); | |
| set.setDuration(700); | |
| set.start(); | |
| } | |
| private void playScaleAnimation(Button scaleButton) { | |
| ObjectAnimator xAnimator = ObjectAnimator | |
| .ofFloat(scaleButton, View.SCALE_X, 1, 2); | |
| xAnimator.setRepeatCount(ValueAnimator.INFINITE); | |
| xAnimator.setRepeatMode(ValueAnimator.REVERSE); | |
| ObjectAnimator yAnimator = ObjectAnimator | |
| .ofFloat(scaleButton, View.SCALE_Y, 1, 2); | |
| yAnimator.setRepeatCount(ValueAnimator.INFINITE); | |
| yAnimator.setRepeatMode(ValueAnimator.REVERSE); | |
| ObjectAnimator rotateAnimator = ObjectAnimator | |
| .ofFloat(scaleButton, View.ROTATION, 0, 270); | |
| rotateAnimator.setRepeatCount(ValueAnimator.INFINITE); | |
| rotateAnimator.setRepeatMode(ValueAnimator.REVERSE); | |
| ObjectAnimator translatorAnimator = ObjectAnimator | |
| .ofFloat(scaleButton, View.TRANSLATION_X, 1, 200); | |
| translatorAnimator.setRepeatCount(ValueAnimator.INFINITE); | |
| translatorAnimator.setRepeatMode(ValueAnimator.REVERSE); | |
| AnimatorSet set = new AnimatorSet(); | |
| set.playTogether(xAnimator, yAnimator, rotateAnimator, translatorAnimator); | |
| set.setDuration(700); | |
| set.start(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's great