0

当我单击一个按钮时,我想在 500 毫秒内更改按钮背景图像并做一个简短的动画。我已经做了一个代码,但我看不到结果,动画还可以,但点击的图像按钮没有出现。

默认按钮图像是“boutton_a.png”单击的按钮图像是“button_a_ok.png”

这是 onclick 按钮代码:

            if (Button04.getText().equals(Reponse)){
                    Button01.setBackgroundResource(R.drawable.boutton_a_ok);
                    AnimationSet set=new AnimationSet(true);
                    Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,10,Animation.RELATIVE_TO_SELF,10);
                    animation.setDuration(100);
                    set.addAnimation(animation);
                    Button01.startAnimation(set);
            }else{
                    Button01.setBackgroundResource(R.drawable.boutton_a_nok);
                    AnimationSet set=new AnimationSet(true);
                    Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,10,Animation.RELATIVE_TO_SELF,10);
                    animation.setDuration(100);
                    set.addAnimation(animation);
                    Button04.startAnimation(set);
            }

我能怎么做 ?

编辑:我已经发布了所有代码,因为有 2 个不同的背景资源

4

2 回答 2

0

您可以通过为 xml 中的不同按钮状态创建可绘制对象来更改按钮单击时的按钮背景。这是示例

<selector 
    <item android:drawable="@drawable/button_pressed_yellow"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused_orange"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal_green" />
</selector>

将此文件保存在可绘制文件夹中为 new_button.xml

<Button
        android:id="@+id/imageButtonSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/new_button" />
于 2013-07-01T06:33:50.200 回答
0

将此文件作为“rotate.xml” res/anim/rotate.xml 放在您的资源文件夹中

并使用这个类来制作动画

public class DownloadButton extends Button
{
    private String state = "Normal";
    public DownloadButton( Context context )
        {
            super(context);
        }
    public DownloadButton( Context context, AttributeSet attrs )
        {
            super(context, attrs);
        }
    public DownloadButton( Context context, AttributeSet attrs, int defStyle )
        {
            super(context, attrs, defStyle);
        }
    public void setState(String state)
        {
            this.state = state;
            if (this.state.equals("Downloading"))
                {
                    ButtonAnimate();
                }
            else if(this.state.equals("Waiting"))
                {
                    setToWaiting(); 
                }
            else
                {
                    clearAnimation();
                }
        }
    private void setToWaiting()
        {
            setBackgroundResource(R.drawable.waiting);
        }
    public void ButtonAnimate()
        {
            this.startAnimation(AnimationUtils.loadAnimation(MediaSingleton.getInstance().getContext(), R.anim.rotate));
        }
    public String getState()
        {
            return state;
        }
}

并像这样在您的布局中使用此按钮

<com.yourpackage.DownloadButton
    android:id="@+id/Downloadbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:layout_marginTop="2dp"
    android:layout_gravity="center_vertical"
    android:background="@drawable/download"
    />

并像这样使用这个动画

Button button=(Button) findViewById(R.id.yourbtnid);
button.setBackgroundResource(R.drawable.downloading);
button.setState("Downloading");

你做了你的动画祝你好运

于 2013-07-01T07:24:05.717 回答