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つのbackgroundressourcesの違いがあるため、すべてのコードを投稿しました

4

2 に答える 2

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 に答える
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>

このファイルを drawable フォルダーに 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 に答える