PeakGen
PeakGen

Reputation: 23025

Left to right movement animation

Please have a look at the following code:

left_to_right.xml (animation)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate android:fromXDelta="-100%" android:toXDelta="0%"
             android:fromYDelta="0%" android:toYDelta="0%"
             android:duration="700"/>
</set>

Java code

 animation2 = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
    animation2.setAnimationListener(new AnimationEvent2());

    private class AnimationEvent2 implements AnimationListener
        {

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                displayIndex++;
                words.setText(wordList.get(displayIndex));

            }

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

            }

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

        }

This animation is applied to a LinearLayout. When activated, it disappears from the right, but it is not a move. What I want is, when activated, the LinearLayout should move from right to left, then disappears from the left corner (still moving until it is 100% away from the view) and when it got disappeared completely, and the same LinearLayout should appear from the left side.

This is just like a HorizontalScroller where it scrolls away allowing the other object to come into the position. But the issue here is, here the same LinearLayout is moving away and coming back.

How can I do this?

Upvotes: 0

Views: 2829

Answers (2)

Venkatesh S
Venkatesh S

Reputation: 5494

left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate android:fromXDelta="-100%" android:toXDelta="0%"
             android:fromYDelta="0%" android:toYDelta="0%"
             android:duration="700"/>
</set>

right_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate
     android:fromXDelta="0%" android:toXDelta="100%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700" />
</set>

Sample Class Understand it ....

package com.test.testproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    LinearLayout linearLayout ;
    Animation left_to_right_animation;
    Animation right_to_left_animation ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = new LinearLayout(this);//intialise your layout
        left_to_right_animation = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
        right_to_left_animation = AnimationUtils.loadAnimation(this, R.anim.right_to_left);
        linearLayout.startAnimation(left_to_right_animation);
        left_to_right_animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

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

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                linearLayout.startAnimation(right_to_left_animation);
            }
        });
        right_to_left_animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

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

            }

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

            }
        });



    }
}

Upvotes: 1

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

I just Use these kind of things ,May be helpful

    AnimationSet set = new AnimationSet(true)       
    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(200);
    set.addAnimation(animation);

    animation = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);

    animation.setDuration(200);
    set.addAnimation(animation);

    LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
    Your_Layout.setLayoutAnimation(controller);

Upvotes: 0

Related Questions