Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Animation programmatically

For example, xml animation:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">

<!-- Rotate -->
<rotate
    android:duration="500"
    android:fromDegrees="30"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="4"
    android:repeatMode="reverse"
    android:toDegrees="0"/>

<!--Move-->
<translate
    android:duration="1000"
    android:fromXDelta="0%"
    android:fromYDelta="150%"
    android:interpolator="@android:anim/linear_interpolator"
    android:toXDelta="0%"
    android:toYDelta="0%"/>

<!--Fade In-->
<alpha
    android:duration="2000"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toAlpha="1.0"/>

</set>

Is it possible to create this via java code?

like image 662
ilw Avatar asked Jul 26 '16 15:07

ilw


1 Answers

Did you tried like that :

// when getting anims 
Animation animRotate = AnimationUtils.loadAnimation(context, R.anim.rotate);
Animation animMove = AnimationUtils.loadAnimation(context, R.anim.move);
Animation animFadeIn = AnimationUtils.loadAnimation(context, R.anim.fade_in);

If you want to do it programmatically :

I am using this way in my project :

view.animate()
            .scaleY(1)
             //just wanted to show you possible methods you can add more
            .rotationY()
            .alpha()
            .setStartDelay(100)
            .rotationX()
            .setDuration(200)
            .setInterpolator(new DecelerateInterpolator())
            .setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            }).start();

Also some other ways according to your xml:

// scale animation
ScaleAnimation scaleanim = new ScaleAnimation(float fromX, float toX, float fromY, float toY);
scaleanim.setDuration(500);

// create translation animation
TranslateAnimation translateanim = new TranslateAnimation(0, 0,
        TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
        TranslateAnimation.ABSOLUTE, yTo - getTop());
translateanim.setDuration(700);

You can use RotateAnimation , AlphaAnimation etc. Try to use them and if you fail or more specific help i will give you my existing project's code.

@Edit : sample TranslateAnimation constructor :

Animation animation = new TranslateAnimation(
float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);
animation.setDuration(1500);
animation.setFillAfter(true);
view.startAnimation(animation);
like image 186
Yasin Kaçmaz Avatar answered Oct 19 '22 06:10

Yasin Kaçmaz