zeg*_*nus 37 animation android alpha fadeout fadein
我想做一个非常简单的alpha动画,但我找不到有效的方法.
我们的想法是在视图上执行此动画:
我试图用AnimationSet实现它:
AnimationSet animationSet = new AnimationSet(true);
Animation animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
animation1.setDuration(1000);
Animation animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
animation2.setDuration(1000);
animation2.setStartOffset(5000);
Animation animation3 = new AlphaAnimation(0.0f, 0.0f);
animation3.setDuration(4000)
animation3.setStartOffset(6000);
animationSet.add(animation1);
animationSet.add(animation2);
animationSet.add(animation3);
Run Code Online (Sandbox Code Playgroud)
等等..
但它接下来第三个动画与所有的alpha动画混乱,我认为这导致Android管理这种类型的动画的方式内部不连贯.
任何的想法?
谢谢.
She*_*tib 107
好请记住这两点来解决这个问题
如果我想1.0f to 0.0f
在动画持续时间为1秒的5秒后进行动画制作,那么这最终是1秒动画,暂停时间为5秒.
为了实现这个目标:
setDuration(1000)
(持续时间为1秒)setStartOffset(5000)
(它将在5秒后开始)你只需要2个永远循环的动画.
1. 0.0f to 1.0f
暂停5秒,持续1秒
2. 1.0f to 0.0f
暂停5秒,持续1秒
以下是代码:
animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(5000);
textView.startAnimation(animation1);
Run Code Online (Sandbox Code Playgroud)
但是要永远循环,我会使用AnimationListener
因为repeatCount是错误的:
animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
//animation1 AnimationListener
animation1.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationEnd(Animation arg0) {
// start animation2 when animation1 ends (continue)
textView.startAnimation(animation2);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(5000);
//animation2 AnimationListener
animation2.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationEnd(Animation arg0) {
// start animation1 when animation2 ends (repeat)
textView.startAnimation(animation1);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
textView.startAnimation(animation1);
Run Code Online (Sandbox Code Playgroud)
ama*_*Bit 17
有一个更简单的解决方案.
让我们假设您的视图处于GONE状态.为其可见性设置动画:
yourView.setVisibility(View.VISIBLE);
yourView.animate().alpha(1).setDuration(300);
Run Code Online (Sandbox Code Playgroud)
通过相同的方式,您可以添加动画侦听器.
这也适用于缩放和平移动画.
归档时间: |
|
查看次数: |
68332 次 |
最近记录: |