0

I want to do an animation in which an image continuously keeps moving from top to bottom after every 3 seconds. I have achieved this using the following code

My XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left|top"
    android:baselineAligned="false"
    android:background="#ffffff">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/layout1">
        </RelativeLayout>
</LinearLayout>

My Activity class

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.Timer;
import java.util.TimerTask;

public class GameActivity extends AppCompatActivity {

    private RelativeLayout layout1;

    private TranslateAnimation moveDownwards;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        layout1 = (RelativeLayout) findViewById(R.id.layout1);

        moveDownwards = new TranslateAnimation(0, 0, -100, 1000);
        moveDownwards.setDuration(3000);
        moveDownwards.setFillAfter(true);

        startTimer();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startTimer();
    }

    private void startTimer() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ImageView iv1 = new ImageView(GameActivity.this);
                        iv1.setImageResource(R.drawable.c_orange);

                        layout1.addView(iv1);

                        iv1.startAnimation(moveDownwards);
                    }
                });
            }
        };
        timer.schedule(task,0,3000);
    }
}

Above code is working fine but the animation is not consistent. For example, when I touch the screen while the image is moving, the smoothness of animation is disturbed and it becomes rough.

Is there any better way to achieve this???

Vishal Afre
  • 1,013
  • 3
  • 12
  • 39

1 Answers1

1

There is no need to add a new Image view after every 3 sec. You can set repeatcount -1(INFINITE).

public class MainActivity extends Activity {

    private TranslateAnimation moveDownwards;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        moveDownwards = new TranslateAnimation(0, 0, -100, 1000);
        moveDownwards.setDuration(3000);
        moveDownwards.setFillAfter(true);
        moveDownwards.setRepeatCount(-1);
        findViewById(R.id.image).startAnimation(moveDownwards);
    }

}



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start|top"
    android:background="#ffffff" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
Ashlesha Sharma
  • 949
  • 7
  • 15
  • Actually I'm going to extend my code such that the image will be randomly chosen from one of the 5 images that I have. So your code won't work in that case – Vishal Afre Feb 23 '16 at 15:46