Johann
Johann

Reputation: 29885

Programmatically change start postion for animation

In my Android app, I use an animation to slide a view up. My resource file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/linear_interpolator">

    <translate
        android:fromYDelta="1300"
        android:duration="700"/>
</set>

and in code I execute the animation like this:

RelativeLayout rlMapContainer = (RelativeLayout) getActivity().findViewById(R.id.rlMapContainer);
Animation animation = AnimationUtils.loadAnimation(context, R.anim.map_anim_up);
rlMapContainer.startAnimation(animation);

I want to change the fromYDelta to a value that I determine at runtime. How can I change this value?

Upvotes: 4

Views: 1602

Answers (2)

Johann
Johann

Reputation: 29885

The following code accomplishes the same thing:

  TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 1200, 0);
  translateAnimation.setInterpolator(new LinearInterpolator());
  translateAnimation.setDuration(800);
  translateAnimation.setFillAfter(true);
  rlMapContainer.startAnimation(translateAnimation);

Upvotes: 0

Samir Bhatt
Samir Bhatt

Reputation: 3261

Instead of using xml file, you may try to animate view programatically. Check my code which may help you for animation.

TranslateAnimation anim1 = new TranslateAnimation(0, 0,
                    500, 0);
            anim1.setDuration(500);
            anim1.setFillAfter(false);

            layTransparent.animate().setDuration(500)
                    .setListener(new AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onAnimationCancel(Animator arg0) {
                            // TODO Auto-generated method stub
                        }
                    });

            layTransparent.startAnimation(anim1);

Upvotes: 1

Related Questions