我是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文档以获得更多详细信息!

编辑:正如@ n​​jzk2所指出的,一种更简单的方法是使用ViewPropertyAnimator,因为您可以像在他的注释中那样链接调用。如果支持API级别16和更高级别,则可以使用withEndAction(Runnable r)方法,否则,可以像在本文开头的代码段中那样使用addListener(AnimatorListener listener)

关于java - 使用计时器更改对象的alpha几秒钟?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30713420/

10-11 04:03
查看更多