Farid Abdi
Farid Abdi

Reputation: 99

How to animate an ImageView from left to fill the screen?

I have an image set in an ImageView and I want to animate it such a way that initially nothing is displayed on the screen and the the image enters from left and go towards right and finally fill the screen

In the code that i examine the image enters from left but doesn't fill the screen and left blank behind itself

ObjectAnimator transAnimation= 
ObjectAnimator.ofFloat(imageView,"translationX",-100,100);
transAnimation.setDuration(3000);//set duration
transAnimation.start();//start animation

Upvotes: 0

Views: 1505

Answers (3)

Pro Mode
Pro Mode

Reputation: 1463

The code for animation that you posted seems to be working fine. The problem that you have is with imageview itself and the place it takes over your screen.

You said that your imageview is not taking full space and leaving blank space behind it, so to fix it simply make your imageview width = match_parent. and if it still doesn't work then add scaleType=centerCrop

UPDATE:

Add this code to your onCreate()

imageView.post(new Runnable() {
        @Override
        public void run() {
            startImageAnimation();
        }
    });


 private void startImageAnimation() {
    ObjectAnimator animation = ObjectAnimator.ofFloat(imageView, "translationX",-(imageView.getWidth()), 0);
    animation.setDuration(1100);
    animation.start();
}

Upvotes: 1

Antonis Radz
Antonis Radz

Reputation: 3097

What about using TransistionManager?

            val slideRight= Slide()
            slideRight.slideEdge = Gravity.RIGHT
            slideRight.mode = Slide.MODE_IN
            slideRight.addTarget(logoImageView)
            slideRight.duration = ANIMATION_DURATION

            TransitionManager.beginDelayedTransition(parentContainer, slideRight)
            logoImageView.visibility = VISIBLE

It is snippet from my working code in Kotlin

Also you can use TransitionSet to combine animations

Upvotes: 0

niceumang
niceumang

Reputation: 1432

step 1: create below left_to_right_anim file in anim directory of res

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

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

step 2:

//your image view
imageView = (ImageView) findViewById(R.id.img);

//name of animation from anim directory
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.left_to_right_anim);

// Start the animation like this
imageView.startAnimation(animSlide);

step 3:

now calculate time of animation and when your imageview animate at right side stop it and set it into your screen. to achieve this you can user handler when you start your animation. Thread's run() method is used to get time of your animation. thanks ;)

Upvotes: 0

Related Questions