使用计时器改变对象的 alpha 几秒钟?

Using a timer to alter the alpha for object for a few seconds?

我是 android 新手,所以这里需要最简单的代码示例, 我有一个布局,在 OnClick 上,我想将 alpha 设置为 0.50 2 秒,然后它会将我导航到另一个页面(有点像按钮阴影效果)。

LinearLayout ChangeThisLayoutAlpha = (LinearLayout) findViewById(R.id.ThisLayout);
ChangeThisLayoutAlpha.setAlpha((float) 0.50);

我如何使用计时器或其他任何东西在 (a) 之间暂停 2 秒,然后设备将我导航到另一个页面(并将 alpha 更改回 1.0)之前将其着色 2 秒?

谢谢!

我想说最简单的方法是使用 ObjectAnimator,例如 tutorial here

您的代码将如下所示:

final LinearLayout ChangeThisLayoutAlpha= (LinearLayout) findViewById(R.id.ThisLayout); 
ObjectAnimator animator = ObjectAnimator.ofFloat(ChangeThisLayoutAlpha, "alpha", 1f, 0.5f);
animator.setDuration(2000);
animator.addListener(new AnimatorListenerAdapter() {
    public void onAnimationEnd(Animator animation) {
       ChangeThisLayoutAlpha.setAlpha(1f);
       // Do your transition to the next screen here
    }
})
animator.start();

不要忘记查看 ObjectAnimator 文档以了解更多详细信息!

编辑:正如@njzk2 指出的那样,一种更简单的方法是使用 ViewPropertyAnimator,因为您可以像他的评论中那样链接调用。如果你支持 API 16 级及以上,你可以使用 withEndAction(Runnable r) 方法,否则你可以使用 post 开头代码片段中的 addListener(AnimatorListener listener) 方法。