Java 使用计时器将对象的alpha更改几秒钟?
我是android新手,所以这里需要最简单的代码示例, 我有一个布局,在点击Java 使用计时器将对象的alpha更改几秒钟?,java,android,timer,Java,Android,Timer,我是android新手,所以这里需要最简单的代码示例, 我有一个布局,在点击时,我想将alpha设置为0.50,持续2秒,然后再将我导航到另一个页面(有点像按钮阴影效果) 如何使用计时器或其他工具在(a)之间暂停2秒,然后设备将我导航到另一页(并将alpha更改回1.0) 谢谢大家! 我想说最简单的方法是使用对象动画制作工具,就像在中一样 您的代码如下所示: final LinearLayout ChangeThisLayoutAlpha= (LinearLayout) findViewById
时,我想将alpha设置为0.50,持续2秒,然后再将我导航到另一个页面(有点像按钮阴影效果)
如何使用计时器或其他工具在(a)之间暂停2秒,然后设备将我导航到另一页(并将alpha更改回1.0)
谢谢大家! 我想说最简单的方法是使用对象动画制作工具,就像在中一样
您的代码如下所示:
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)
方法,否则您可以使用addListener(AnimatorListener)
类似于本文开头的代码片段。我想说最简单的方法是使用ObjectAnimator
,就像在
您的代码如下所示:
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)
方法,否则您可以使用addListener(AnimatorListener)
就像本文开头的代码片段一样。2秒是很长的时间是的,测试只需要2秒,您也可以使用ViewPropertyAnimator
,例如:ChangeThisLayoutAlpha.animate().alpha(0.5).setDuration(2000).withEndAction(this::goToNextPage)
(goToNextPage
是进行导航的方法)(在retrolambda的一点帮助下)2秒是很长的时间耶,测试只需要2秒钟,计划随后更改。您也可以使用ViewPropertyAnimator
,如下所示:ChangeThisLayoutAlpha.animate().alpha(0.5).setDuration(2000).withEndAction(this::goToNextPage)
(goToNextPage
是导航的方法)(在retrolambda的帮助下)2秒是一段很长的时间是的,测试只需要2秒,计划在之后更改。您也可以使用ViewPropertyAnimator
,如下所示:ChangeThisLayoutAlpha.animate().alpha(0.5).setDuration(2000).withEndAction(this::goToNextPage)
(goToNextPage
是您进行导航的方法)(由retrolambda提供一些帮助)我发现ViewPropertyAnimator
更容易用于那种用例我发现ViewPropertyAnimator
更容易用于那种用例我发现ViewPropertyAnimator
更容易用于那种用例