Android连续移动动画 [英] Android continuous moving animation

查看:711
本文介绍了Android连续移动动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个动画,其中图像每3秒连续不断地从上到下移动.我使用以下代码实现了这一点

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

我的XML文件

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

我的活动课程

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

推荐答案

不需要每3秒钟添加一个新的Image视图.您可以设置重复计数-1(INFINITE).

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>

这篇关于Android连续移动动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆