2014-09-19 109 views
0

我想使Mediaplayer按钮在按下后开始闪烁,并停止闪烁,直到我按下下一个Mediaplayer按钮,该按钮也开始闪烁。现在我按下按钮时现在闪烁,但现在的问题是,当我按下下一个按钮时,如何让它们停止闪烁。现在当我按下下一个按钮时,按钮会继续闪烁。如何让按钮按下后闪烁?

这里是我的代码,我现在使用:

 mButton1 = (Button)findViewById(R.id.button1); 
     mButton2 = (Button)findViewById(R.id.button2); 

     mAnimation = new AlphaAnimation(1, 0); 
     mAnimation.setDuration(500); 
     mAnimation.setInterpolator(new LinearInterpolator()); 
     mAnimation.setRepeatCount(Animation.INFINITE); 
     mAnimation.setRepeatMode(Animation.REVERSE); 

     mButton1.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View v) { 
       mButton2.clearAnimation(); 
       mButton1.startAnimation(mAnimation); 

       { 
        mp.release(); 
        mp = MediaPlayer.create(Activity2.this, R.raw.audio_1); 
        mp.setLooping(true); 
        mp.start(); 


       } 
       } 
     }); 

     mButton2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mButton1.clearAnimation(); 
       mButton2.startAnimation(mAnimation); 

       { 
       mp.release(); 
       mp = MediaPlayer.create(Activity2.this, R.raw.audio_2); 
       mp.setLooping(true); 
       mp.start(); 


      } 
      } 
     }); 
+0

你能解释一点吗?你是否想从一组按钮中只闪烁一个按钮?当按下一个按钮时,它会开始闪烁,之前闪烁的按钮应该停止? – dishan 2014-09-19 13:56:04

+0

正是我在找! – 2014-09-19 13:59:35

+0

我有Mediaplayer与12 differend按钮/音频,我希望按钮一次闪烁一个,这取决于我按哪个按钮。 – 2014-09-19 14:12:50

回答

0

试试这个办法

mButton1 = (Button)findViewById(R.id.button1); 
mButton2 = (Button)findViewById(R.id.button2); 

mAnimation = new AlphaAnimation(1, 0); 
mAnimation.setDuration(500); 
mAnimation.setInterpolator(new LinearInterpolator()); 
mAnimation.setRepeatCount(Animation.INFINITE); 
mAnimation.setRepeatMode(Animation.REVERSE); 

mBbutton1.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     mButton2.clearAnimation(); 
     mButton1.startAnimation(mAnimation); 
    } 
}); 

mBbutton2.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     mButton1.clearAnimation(); 
     mButton2.startAnimation(mAnimation); 
    } 
}); 

注意,在Button1的点击收听你应该开始在Button1的动画和清除按钮2,反之亦然动画

如果你有很多按钮尝试将它们存储在一个数组中并执行逻辑

mButtons = new ArrayList<Button>(); 

mClickListener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //stop flashing all buttons 
     for(Button button : mButtons) { 
      button.clearAnimation(); 
     } 
     //start animation on this button 
     v.startAnimation(mAnimation); 
    } 
}); 

Button button1 = (Button)findViewById(R.id.button1); 
button1.setOnClickListener(mClickListener); 
Button button2 = (Button)findViewById(R.id.button2); 
button2.setOnClickListener(mClickListener); 
//get as many as buttons 

mButtons.add(button1); 
mButtons.add(button2); 
//add all the buttons to array 
+0

感谢您的帮助,如果您现在使用3种不同的按钮/音频查看我的代码。我应该如何开始构建它。我有尝试,但我得到很多错误.. – 2014-09-19 17:13:03

+0

嗨,我终于得到按钮正确闪烁,但现在我听不到音频文件。你能否告诉我我做错了什么。检查我编辑的代码? – 2014-09-20 12:17:43

+0

运行音频播放的代码不会在按钮点击监听器中调用。添加代码以在每个点击监听器中的startAnimation()方法之后运行音频。这是你的btn1()和btn2()方法中的一段代码 – dishan 2014-09-20 13:08:35