gpt4 book ai didi

android - 使 ImageView 在 2 秒内出现

转载 作者:行者123 更新时间:2023-11-30 01:24:15 25 4
gpt4 key购买 nike

我有一个 ImageView,我希望它在代码运行后 2 秒出现。我试过这段代码,但不幸的是它没有做任何事情。

ImageView aniView = (ImageView) findViewById(R.id.imageView1);
float dest = 1;
if (aniView.getAlpha() <= 0) {
dest = 0;
System.out.println("F");
}
ObjectAnimator animation3 = ObjectAnimator.ofFloat(aniView,
"alpha", dest);
animation3.setDuration(5000);
animation3.start();

最佳答案

不需要任何HandlerCountDownTimer 或任何其他直接线程操作。

Android 内置类如 ObjectAnimatorViewPropertyAnimator 在其 API 中包含所有需要的方法。

使用ObjectAnimator:

  private static void showImageViewObjectAnimator(@NonNull final ImageView imageView) {
imageView.setAlpha(0f);
final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);
objectAnimator.setStartDelay(TimeUnit.SECONDS.toMillis(2));
objectAnimator.setDuration(TimeUnit.SECONDS.toMillis(5));
objectAnimator.start();
}

使用ViewPropertyAnimator:

  private static void showImageViewViewPropertyAnimator(@NonNull final ImageView imageView) {
imageView.setAlpha(0f);
imageView.animate()
.alpha(1f)
.setStartDelay(TimeUnit.SECONDS.toMillis(2))
.setDuration(TimeUnit.SECONDS.toMillis(5));
//no need to call start!
}

希望对你有帮助

关于android - 使 ImageView 在 2 秒内出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36664724/

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