Android的 - 我怎样才能使图像的动画发生在一定的时间间隔? [英] Android - How can I make an animation of an image occur at a certain interval?

查看:350
本文介绍了Android的 - 我怎样才能使图像的动画发生在一定的时间间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在证书课程班在Android编程。这里是什么我试图做一个说明...

I'm taking a Coursera class on Android programming. Here's a illustration of what I'm trying to do...

这里的code我到目前为止...

Here's the code I have so far...

XML:

<Button
        android:id="@+id/startbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/leftfoot"
        android:layout_alignRight="@+id/leftfoot"
        android:onClick="startRhythmandAnimation"
        android:text="@string/start_button" />

Java的:

public class Assignment3MainActivity extends Activity {

private View mMileTimeGoal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_assignment3_main);
    mMileTimeGoal = findViewById(R.id.miletimegoal);
}

public void startRhythmandAnimation () {
    String MileTime = mMileTimeGoal.getContext().toString();
    String[] time_array = MileTime.split(":");
    int hours = Integer.parseInt(time_array[0]);
    int minutes = Integer.parseInt(time_array[1]);
    int seconds = Integer.parseInt(time_array[2]);
    int duration = 3600 * hours + 60 * minutes + seconds;
    int steps_per_second = 3;

    int running_rate = duration * steps_per_second;

    View rightfoot = findViewById(R.id.rightfoot);
    View leftfoot = findViewById(R.id.leftfoot);

    rightfoot.setVisibility(View.VISIBLE);
    Animation anim = AnimationUtils.makeInChildBottomAnimation(this);
    rightfoot.startAnimation(anim);

    leftfoot.setVisibility(View.VISIBLE);
    leftfoot.startAnimation(anim);
}

这是如何形成我的算法这将滑动和淡出我的看法rightfoot和leftfoot查看任何想法?

Any ideas on how to form my algorithm which would slide and fade my rightfoot view and leftfoot view?

我应该使用whil​​e循环,并揭开序幕某种类型的计时器?

Should I use a while loop and kick off some type of timer?

推荐答案

活动

private Handler mHandler;    
private long mInterval = 1000;
private View mLeftfoot;
private Animation mFootAnim;

public void onCreate(Bundle bundle) {
   ...
   mHandler = new Handler(); //.os package class when importing
   mLeftfoot = findViewById(R.id.leftfoot);
   mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot);
   stepRecursive();
}

private void stepRecursive() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mLeftFoot.startAnimation(mFootAnim );
            stepRecursive();
        }
    }, mInterval);
}

/res/anim/foot.xml

/res/anim/foot.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="-15" android:duration="400"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="400" />
</set>

多数民众赞成直客我的头顶(因此未经测试),但应该有很多让你在正确的方向前进

Thats straight off the top of my head (thus untested) but should be plenty to get you going in the right direction

这篇关于Android的 - 我怎样才能使图像的动画发生在一定的时间间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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