我有一个 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();
最佳答案
不需要任何Handler
、CountDownTimer
或任何其他直接线程操作。
Android 内置类(如 ObjectAnimator
或 ViewPropertyAnimator
)在其 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/