user4768719
user4768719

Reputation:

How to animate a view programatically in android?

I am making an animated tutorial for the application. I am using a fragment over the activity to show image view sliding effect. I am using Translate animation to move the image view to the specific view to describe its working, but actually I want to animate the image view to move infinite times after reaching to the view.

Upvotes: 1

Views: 550

Answers (1)

Amol Patil
Amol Patil

Reputation: 491

Assume that view v is suppose to animate

    public void ViewExapand(View v)
        {
                        v.setVisibility(View.VISIBLE);

                        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        v.measure(widthSpec, heightSpec);
                        ValueAnimator mAnimator = slideAnimator(0, v.getMeasuredHeight(),v);
                        mAnimator.start();
        }



private ValueAnimator slideAnimator(int start, int end, final View v)
    {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }

You can also use ObjectAnimator

ObjectAnimator.ofInt(your view, "left", 100, 250)
    .setDuration(250)
    .start();

Upvotes: 1

Related Questions