2016-11-15 15 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と真はフェードを行う場合は、ブール引数を渡します - アニメーションの他のフェードアウトアニメーション。コードサンプルを以下に示します。使用法fadeAnimation(true)fadeInアニメーションの場合はfadeAnimation(false)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(); 
関連する問題