Skip to content Skip to sidebar Skip to footer

How Can I Make This Code For Animations More Effective?

This code switches the y-position of two Views (mView1 and mView2) on Button click via ObjectAnimator and AnimationSets. While the translate animation the alpha value of both views

Solution 1:

You will want to put the animation code inside the class that's animated ... like this :

publicclassFadingViewextendsView {

    private ObjectAnimator fade, moveX, moveY;
    private AnimatorSet moveSet;

    publicFadingView(Context context) {
        super(context);
    }

    privatevoidfade(int duration, float...values) {

        if(fade==null) {
            fade = ObjectAnimator.ofFloat(this, "alpha", values);
        } else {
            fade.setFloatValues(values);
        }
        fade.setDuration(duration);
        fade.start();

    }

    privatevoidmove(int duration, int x, int y) {

        if(moveX==null) {
            moveX = ObjectAnimator.ofFloat(this, "translation_x", x);
        } else {
            moveX.setFloatValues(x);
        }
        if(moveY==null) {
            moveY = ObjectAnimator.ofFloat(this, "translation_y", y);
        } else {
            moveY.setFloatValues(y);
        }
        if(moveSet == null) {
            moveSet = newAnimatorSet();
            moveSet.playTogether(moveX, moveY);
        }

        moveSet.setDuration(duration);
        moveSet.start();

    }

    publicvoidmoveToUpperLeft(int duration) {
        move(duration, 0,0);
    }

    publicvoidshow(int duration) {
        fade(duration,1);
    }

    publicvoidhide(int duration) {
        fade(duration,0);
    }

}

Just a basic example, but now you can call :

FadingViewview=newFadingView(context);
view.hide(500);
view.moveToUpperLeft(500);

Of course you can customize and generalize this even more ...

Post a Comment for "How Can I Make This Code For Animations More Effective?"