Skip to content

Instantly share code, notes, and snippets.

@vipulshah2010
Created December 29, 2017 10:45
Show Gist options
  • Select an option

  • Save vipulshah2010/9d33ccc07897758db01b7d7fac2e692a to your computer and use it in GitHub Desktop.

Select an option

Save vipulshah2010/9d33ccc07897758db01b7d7fac2e692a to your computer and use it in GitHub Desktop.
Object Animators in Android
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();
}
}
Copy link

ghost commented Dec 29, 2017

This is the xml file

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gholakemohit.myapplication.MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="135dp">

    <Button
        android:id="@+id/alphaButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="alpha"/>
    <Button
        android:id="@+id/rotateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="rotate"/>
    <Button
        android:id="@+id/translateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="translate"/>
    <Button
        android:id="@+id/scaleButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scale"/>
</LinearLayout>

</android.support.constraint.ConstraintLayout>

@jjasonsolomon
Copy link

That's great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment