How To Make toRightOf/toLeftOf Animation?

I have 3 items in my layout. A text in center horizontal, an imageview1 left of text, an imageview 2 right of text.

My Layout

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text=""
    android:layout_centerHorizontal="true"
    android:id="@+id/mainTextView1"/>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/hub"
    android:layout_toRightOf="@id/mainTextView1"
    android:id="@+id/mainImageView2"/>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/logo"
    android:layout_toLeftOf="@id/mainTextView1"
    android:id="@+id/mainImageView1"/>

My Animation Try:

float ustY;
float altY;
public void animation(){
    ust.setVisibility(View.GONE);
    alt.setVisibility(View.GONE);
    ustY=ust.getX();
    ust.animate().y(0).x(-5000).setDuration(500).start();
    altY=alt.getX();
    alt.animate().y(0).x(4000).setDuration(500).start();
}
public void reverseAnimation(){
    ust.setVisibility(View.VISIBLE);
    alt.setVisibility(View.VISIBLE);
    ust.animate().y(0).x(ustY).setDuration(500).start(); 
    alt.animate().y(0).x(altY).setDuration(500).start(); 
}

And I want to make an animation same as this one:

http://i.eyimg.com/M5AvDKXm.gif

And the result:

Answers


You could create an animation class such as below, which includes methods for sliding in from left or right, and sliding out to left or right...

public class MyAnimator {

    private static int DEFAULT_DURATION = 500;

    public static Animation inFromRightAnimation(long duration) {
        return constructSlideAnimation(1.0f, 0, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    public static Animation inFromLeftAnimation(long duration) {
        return construct(-1.0f, 0, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    public static Animation outToRightAnimation(long duration) {
        return construct(0, 1.0f, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    public static Animation outToLeftAnimation(long duration) {
        return construct(0, -1.0f, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    private static Animation construct(float fromX, float toX, float fromY, float toY, long duration) {
        Animation animation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, fromX, Animation.RELATIVE_TO_PARENT, toX,
            Animation.RELATIVE_TO_PARENT, fromY, Animation.RELATIVE_TO_PARENT, toY
        );
        animation.setDuration(duration);
        animation.setInterpolator(new LinearInterpolator());
        return animation;
    }

}

You can then add whatever else you like to this class if/when you need to, such as fade animations, blink animations, rotate animations, etc, and just call the methods from your activities/fragments like so:

porn.setAnimation(MyAnimator.inFromLeftAnimation(800));
hub.setAnimation(MyAnimator.inFromRightAnimation(800));

The value 800 above is your duration parameter in milliseconds; if you set it to zero (0), then the MyAnimation class uses the DEFAULT_DURATION value (500). You can of course set your default duration to whatever you prefer and always pass zero as a parameter or override it to more/less depending on your preferences for each view you animate.

For the proper effect, also set your two ImageViews to android:visibility="gone" and make them "visible" when you call the slide-in animations. If you wanted to make them slide out, you would make them "gone" when calling the slide-out animations:

SLIDE-IN:

porn.setVisibility(View.VISIBLE);
porn.setAnimation(MyAnimator.inFromLeftAnimation(800));

hub.setVisibility(View.VISIBLE);
hub.setAnimation(MyAnimator.inFromRightAnimation(800));

SLIDE-OUT:

porn.setVisibility(View.GONE);
porn.setAnimation(MyAnimator.outToLeftAnimation(800));

hub.setVisibility(View.GONE);
hub.setAnimation(MyAnimator.outToRightAnimation(800));

Try xml

res/anim/


<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>

In your Activity code:

this.overridePendingTransition(R.anim.animation_enter,
                   R.anim.animation_leave);

Need Your Help

Find out corresponding escape sequence for a given key combo

emacs key-bindings iterm2

In Emacs, I want to bind a particular key combination to a command. However, because I am using Emacs in terminal mode within iTerm2 on OS X, I need to translate the key combo to character escape

Non-deprecated replacement for NSCalibratedBlackColorSpace?

objective-c macos cocoa quartz-graphics deprecated

I'm implementing my own NSBitmapImageRep (to draw PBM image files). To draw them, I'm using NSDrawBitmap() and passing it the NSCalibratedBlackColorSpace (as the bits are 1 for black, 0 for white).