gpt4 book ai didi

Android - 如何让图像的动画以特定的时间间隔出现?

转载 作者:行者123 更新时间:2023-11-29 17:52:59 25 4
gpt4 key购买 nike

我正在参加有关 Android 编程的 Coursera 类(class)。这是我正在尝试做的事情的例证...

Slide and fade interval

这是我到目前为止的代码...

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);
}

关于如何形成我的算法以滑动和淡化我的右脚 View 和左脚 View 的任何想法?

我应该使用 while 循环并启动某种类型的计时器吗?

最佳答案

Activity

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

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

这就是我的想法(因此未经测试),但应该足以让您朝着正确的方向前进

关于Android - 如何让图像的动画以特定的时间间隔出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21667916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com