android:按钮的按下和非按下状态

时间:2013-12-23 14:06:10

标签: android animation button

我正在设计一种游戏,其中有一些导弹(按钮)从屏幕的上方落到屏幕的底部。在屏幕的底部,有5个目标(也是按钮)。

游戏很简单,当导弹与下面的目标重叠时,导弹和目标都将执行动画。

导弹正在延伸视野。

延长晚会观点:

导弹行动

           spot.setBackgroundResource(R.anim.animation_explosion); 
           alert_progress_icon_giraffe = (AnimationDrawable) spot.getBackground();
           alert_progress_icon_giraffe.start();
           score ++;

           mGame_gala.pressing_animation(a);

Game_gala:

public void pressing_animation(int idd_) 
{ 
    btn_up = ((Button) findViewById(idd_));     
    btn_up.setBackgroundResource(R.anim.animation_press_button); 
    alert_pressing_button = (AnimationDrawable) btn_up.getBackground();
    alert_pressing_button.start();
}   

Animation_press_button.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/pressing_button" android:duration="50"/>
    <item android:drawable="@drawable/pressing_button_pressed1" android:duration="50"/>
    <item android:drawable="@drawable/pressing_btn1" android:duration="50"/>

</animation-list>

Drawable pressing_btn1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/pressing_button_pressed1"
          android:state_pressed="true" />    

    <item android:drawable="@drawable/pressing_button" />
</selector>

上述机制:

底部目标,没有导弹重叠在它上面,按下时可以执行按下和非按下的图片。然而,当导弹位于目标顶部时,此时,2个按钮彼此重叠,并且只有导弹可以执行按压和非按压状态pic。所以我已经实现了上述功能,当按下导弹时,它会调用Game_gala方法使目标成为按下的状态图形。

问题:

当导弹第一次出现时,导弹和目标都完美地完成了它们的压制效果。然而,当第二枚导弹击中同一目标时,只有导弹可以执行自己的动画,目标不显示“按下”状态图形。

然而,如果目标是独立按下的,那么即使被导弹按下,也可以完美地显示受压状态和非按压状态。

如何修改上述内容,以便每当导弹重复击中同一目标时,目标会在导弹被按下时显示其按下状态?

简而言之,我希望按下2个重叠按钮显示按下状态。

谢谢!

1 个答案:

答案 0 :(得分:1)

我不知道是不是因为btn_up被夸大了,第一次没有得到btn_up的id ...?

尽管如此,我使用Handler而不是使用AnimationDrawable制定了一种简单的方法,这种方法更加简单并带来了相同的最终效果:

public void pressing_animation(int iddd_) 
{       
    btn_up = ((Button) findViewById(iddd_));    
    btn_up.setPressed(true);
    final Handler handler3 = new Handler();
    handler3.postDelayed(new Runnable() 
    {
        @Override
        public void run() 
        {
            btn_up.setPressed(false);
        }
    }, 50);
}