Java 动画延迟
我有三个Java 动画延迟,java,android,animation,Java,Android,Animation,我有三个按钮,我希望它们在单击“开始”按钮后随机改变颜色。我的问题是,我无法让动画一个接一个地显示,它们最终会互相重叠 我知道我必须使用animationListiner(),但我不能完全理解它,也不能让它工作 private void startColorAnimation(View v) { for(int i =0; i<10;i++) { final int min = 0; final int max = 2;
按钮
,我希望它们在单击“开始”按钮后随机改变颜色。我的问题是,我无法让动画一个接一个地显示,它们最终会互相重叠
我知道我必须使用animationListiner()
,但我不能完全理解它,也不能让它工作
private void startColorAnimation(View v) {
for(int i =0; i<10;i++) {
final int min = 0;
final int max = 2;
final int random = rnd.nextInt((max - min) + 1) + min;
if(random == 0){
animStart = 0xFFFF0000;
animEnd = 0xFF990000;
colorAnim = ObjectAnimator.ofInt(redButton, "backgroundColor", animStart, animEnd );
}else if(random ==1){
animStart = 0xFF3366FF;
animEnd = 0xFF000099;
colorAnim = ObjectAnimator.ofInt(blueButton, "backgroundColor", animStart, animEnd );
}else if(random==2){
animStart = 0xFF009900;
animEnd = 0xFF66CC00;
colorAnim = ObjectAnimator.ofInt(greenButton, "backgroundColor", animStart, animEnd );
}
colorAnim.setDuration(500);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(0);
colorAnim.start();
}
}
private void startColoration(视图五){
对于(int i=0;i,看起来您打算在三个按钮上随机运行总共10个动画
由于动画的持续时间设置为500毫秒,因此需要将给定按钮的下一个动画延迟至少500毫秒
我能想到的实现这一点的唯一方法是跟踪挂起/正在运行的动画,并在实际运行动画时使用Handler.postDelayed(Runnable,delay)
protected void randomizeTheButton(int animCount)
{
int delay = 0;
for(int i = 0; i < animCount; i++)
{
final ObjectAnimator colorAnim = ObjectAnimator.ofArgb(mButton, "backgroundColor", Color.argb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
colorAnim.setDuration(500);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(0);
mHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
colorAnim.start();
}
}, delay);
delay += colorAnim.getDuration();
}
}
protectedvoid randomizeTheButton(int animCount)
{
int延迟=0;
对于(int i=0;i
通过在按钮的click listener中调用此方法,您将获得以下行为。您需要比我在这里显示的更进一步,以便您可以实际运行不同按钮的不同动画
我在代码中实现了处理程序,但现在只有一个按钮(随机)更改颜色。我对线程没有太多经验,我是否必须在那里添加一些额外的内容才能使其工作?非常好。请记住,处理程序实际上不会导致任何多线程。它只是延迟处理程序初始化时线程上的代码执行。有关更多信息,我建议观看此技术k来自最近一个名为“主线程如何工作”的droidcon