简体   繁体   中英

How do I make imageView move infinitely from side to side of the screen? [android animation]

Here is my xml code:

<?xml version="1.0" encoding="utf-8"?>

android:repeatCount="infinite">

    <translate  xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="10"
        android:toXDelta="500"
        android:fillAfter="true"
        android:duration="1000"
        >
    </translate>

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="500"
        android:toXDelta="10"
        android:duration="1000"
        android:fillBefore="true"
        android:startOffset="2000"
        >
    </translate>

here is java code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page1);
    cloud2 = (ImageView) findViewById(R.id.cloud2);
    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clouds_animation);
    cloud2.startAnimation(animation);
    animation.setRepeatCount(Animation.INFINITE);

I have tried already

  • to put "repeatCount=infinite" into both translate, but no result
  • to put programatically setrepeatCount in java code.

What am i doing wrong?

instead of doing 2 animations in xml file, i solved this problem this way:

   TranslateAnimation cloud_moving = new TranslateAnimation(
            Animation.ABSOLUTE, 1450,
            Animation.ABSOLUTE, 10,
            Animation.ABSOLUTE, 0,
            Animation.ABSOLUTE, 0
    );

    cloud_moving.setDuration(6000);
    cloud_moving.setFillAfter(true);
    cloud_moving.setStartOffset(1000);
    cloud_moving.setRepeatCount(Animation.INFINITE);
    cloud_moving.setRepeatMode(Animation.REVERSE);
    cloud2.startAnimation(cloud_moving);

apparently, xml equivalent animation does not work as well as just source code, coding it up works better! from the docs: if you set repeat count to infinite AND set repeat mode to reverse - it will "mirror" itself, ie the image will move from one side to another, and don't forget to set fill after to true - the image will stay where it stays after animation is complete...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM