Android continuous moving animation

情到浓时终转凉″ 提交于 2020-01-17 06:44:14

问题


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


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


来源:https://stackoverflow.com/questions/35578345/android-continuous-moving-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!