2016-11-15 33 views
1

我有这个开关按钮,当它打开时,调用一个动画方法,设置几个可见的元素。但是当我关闭它时,这些元素仍然可见,尽管是相反的指令。我怎样才能让他们失去同样的逻辑?我是否需要创建另一种方法?谢谢,下面的代码:如何使用动画设置元素不可见?

drum.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       key1.setVisibility(View.VISIBLE); 
       key1.startAnimation(fadeInAnimation()); 

       key2.setVisibility(View.VISIBLE); 
       key2.startAnimation(fadeInAnimation()); 

       key3.setVisibility(View.VISIBLE); 
       key3.startAnimation(fadeInAnimation()); 


       rocking.setLooping(true); 
       rocking.start(); 

       Toast.makeText(getApplicationContext(), "Rock and Rolling!", Toast.LENGTH_SHORT).show(); 
      } else { 
       rocking.setLooping(false); 
       key1.setVisibility(View.INVISIBLE);// These instrucions are ignored... 
       key2.setVisibility(View.INVISIBLE); 
       key3.setVisibility(View.INVISIBLE); 

       Toast.makeText(getApplicationContext(), "Can't keep up? Try the tamborine!", Toast.LENGTH_SHORT).show(); 

      } 
     } 
    }); 

与动漫方法:

private Animation fadeInAnimation() { 
Animation animation = new AlphaAnimation(0f, 1.0f); 
animation.setDuration(1000); 
animation.setFillEnabled(true); 
animation.setFillAfter(true); 
return animation; 
} 

回答

3

更改fadeInAnimation并传递一个布尔的说法,如果真做褪色 - 在动画中淡出动画。代码示例如下。用于fadeIn动画的fadeAnimation(true)和用于fadeOut动画的fadeAnimation(false)。希望这可以帮助。

private Animation fadeAnimation(boolean fadeIn) { 

Animation animation = null; 
if(fadeIn) 
    animation = new AlphaAnimation(0f, 1.0f); 
else 
    animation = new AlphaAnimation(1.0f, 0f); 
animation.setDuration(1000); 
animation.setFillEnabled(true); 
animation.setFillAfter(true); 
return animation; 

} 
+0

工作得很好Swathin,感谢您的帮助! – glassraven

+0

高兴地帮助:) – Swathin

0

结帐下面的代码

viewObject.animate() 
      .alpha(0.0f) 
      .setStartDelay(10000) 
      .setDuration(2000) 
      .setListener(new AnimatorListenerAdapter() { 
       @Override 
       public void onAnimationEnd(Animator animation) { 
        super.onAnimationEnd(animation); 
        // do your stuff if any, after animation ends 
       } 
      }).start(); 
相关问题