我想要2个带2个独立动画的按钮

时间:2013-05-30 08:55:37

标签: android animation button

我想拥有2个不同XML动画文件的按钮。

我首先尝试了这个代码:

package com.example.animateabutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button mybutton = (Button)findViewById(R.id.button1);
        mybutton.setOnClickListener(this);

        Button mybutton2 = (Button)findViewById(R.id.button2);
        mybutton2.setOnClickListener(this);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub

        Animation shake = AnimationUtils.loadAnimation(this, R.anim.rshake);
        findViewById(R.id.button1).startAnimation(shake);


        Animation vanish = AnimationUtils.loadAnimation(this ,R.anim.vanish); 
        findViewById(R.id.button2).startAnimation(vanish);


    }

}

我把它改成了这个

package com.example.animateabutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button mybutton = (Button)findViewById(R.id.button1);
        Button mybutton2 = (Button)findViewById(R.id.button2);

            mybutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Animation shake = AnimationUtils.loadAnimation(this, R.anim.rshake);
                findViewById(R.id.button1).startAnimation(shake);
            }
        });

            mybutton2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Animation vanish = AnimationUtils.loadAnimation(this ,R.anim.vanish); 
                findViewById(R.id.button2).startAnimation(vanish);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是vanish.xml动画文件

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

       <scale android:duration="300"
         android:fromXScale="1.0"
         android:fromYScale="1.0"
         android:toXScale="3"
         android:toYScale="3"
         android:pivotX="40%"
         android:pivotY="40%"
         android:interpolator="@android:anim/anticipate_overshoot_interpolator"

        />

    <alpha android:duration="300"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:interpolator="@android:anim/bounce_interpolator"

        />

</set>

这是rshake.xml动画文件

      <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:toXDelta="20"
    android:duration="1000"
  android:interpolator="@anim/cycleby" >

</translate>

这是cycleby.xml

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

这是布局文件activity_main.xml

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip"
    tools:context=".MainActivity" >

  <Button
      android:id="@+id/button1"
      style="@string/shakepress"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="20dip"
      android:hint="@string/shakepress"
      android:text="@string/shakepress" />

 <Button
     android:id="@+id/button2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="@string/vanishpress"
     android:scrollHorizontally="true"
     android:text="@string/vanishpress" />

</LinearLayout>

我希望动画能够为每个按钮单独工作。

1 个答案:

答案 0 :(得分:2)

这样做:

public class MainActivity extends Activity {

Button mybutton,mybutton2;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mybutton = (Button)findViewById(R.id.button1);
        mybutton2 = (Button)findViewById(R.id.button2);

            mybutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                // the Animation files were kept inside animator folder of res. 
                // Hence R.animator.shake and R.animator.vanish. in your case it could be R.anim.rshake and R.anim.vanish

                Animation shake = AnimationUtils.loadAnimation(MainActivity.this, R.animator.shake);
                mybutton.startAnimation(shake);
            }
        });

            mybutton2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Animation vanish = AnimationUtils.loadAnimation(MainActivity.this ,R.animator.vanish); 
                mybutton2.startAnimation(vanish);
            }
        });
    }
}
  

在布局文件中更正此内容。

<!-- Check out the Style attribute of button1. it must be @style--> 

  <Button android:id="@+id/button1"
  style="@style/shakepress" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginBottom="20dip"
  android:hint="@string/shakepress"
  android:text="@string/shakepress" />