28

I'd like to translate an image from left to right on emulator using android animation. I'm new to android animation. How could I do that?

Thanks.

Thomas
  • 24,234
  • 6
  • 81
  • 125
sanjay
  • 2,590
  • 17
  • 55
  • 88

4 Answers4

60
    ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll.setOrientation(LinearLayout.VERTICAL);

    tv = new TextView(this);
    tv.setText("Animation");

    moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
    moveLefttoRight.setDuration(1000);
    moveLefttoRight.setFillAfter(true);

    button = new Button(this);
    button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    button.setText("PressMe");
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            tv.startAnimation(moveLefttoRight);
        }

    });

    ll.addView(tv);
    ll.addView(button);
    setContentView(ll);

is one way of doing it.

techi.services
  • 8,473
  • 4
  • 39
  • 42
  • Hi..Thanks for ur reply.I tried with ur above example.But it displays only the word "Animation" without any move. – sanjay Jan 14 '11 at 10:46
  • 1
    @Sudhakar. Sorry missed off the `Animation.setDuration(1000)` and `Animation.setFillAfter(true)`. – techi.services Jan 14 '11 at 11:20
  • @Pete. Apologies. I will remove the link from the answer. Thanks for the heads up ;). – techi.services Jul 05 '12 at 20:59
  • @techiServices how to animate two texts from middle to opposite direction ? – Juned Apr 24 '13 at 11:05
  • @juned. create two animations eg centerLeft and centerRight with the correct values for the screen size. so center ie start would be screenwidth/2. left edge is 0, right edge is screenwidth. If that is not clear can you post a new question? – techi.services Apr 24 '13 at 11:58
  • @techiServices Thanks for response here is my new question http://stackoverflow.com/questions/16191948/how-to-animate-two-image-view-from-center-to-opposite-to-each-other – Juned Apr 24 '13 at 12:23
35

Move an image from left to right and right to left by using Android TranslateAnimation

enter image description here

ImageView img_animation = (ImageView) findViewById(R.id.img_animation);

    TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
            0.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
    animation.setDuration(5000);  // animation duration 
    animation.setRepeatCount(5);  // animation repeat count
    animation.setRepeatMode(2);   // repeat animation (left to right, right to left )
    //animation.setFillAfter(true);      

    img_animation.startAnimation(animation);  // start animation 

you can find more details from here

Chathura Wijesinghe
  • 3,310
  • 3
  • 25
  • 41
0

I am a bit late to the party but worth answering here,

case 1:

if your view is at the left side of the screen and you want to move from left edge to right edge then use this:

imageView.animate()
            .translationX(((rootLayout.width - (imageView.width))).toFloat())
            .setInterpolator(AccelerateInterpolator()).duration = 1500

case 2: If your view is at the center of the screen and you want to move from center to right edge then use this:

imageView.animate()
            .translationX(((rootLayout.width - (imageView.width)) / 2).toFloat())
            .setInterpolator(AccelerateInterpolator()).duration = 1500

NOTE: rootLayout is root view of your XML

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
-2

Add this code R.anim folder

<?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="100%p"
    android:duration="800" />
</set>
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37