按下后如何使按钮闪烁?

时间:2014-09-19 13:26:34

标签: java android

我想让按下后开始闪烁的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();


            }
            }
        });

1 个答案:

答案 0 :(得分: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中启动动画并清除button2的动画,反之亦然

如果您有很多按钮,请尝试将它们存储在数组中并执行逻辑

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