2013-05-30 97 views
2

我想擁有2個不同的XML動畫文件的按鈕。我想擁有2個按鈕和2個獨立的動畫

我想這段代碼第一:

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> 

我希望動畫爲每個按鈕單獨控制。

+0

嘗試改變findViewById(R.id.button1).startAnimation(抖動); to myButton.startAnimation(shake); – npace

+0

我試過了,但它不起作用,它不能先解決 – user2435575

回答

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" /> 
+0

,感謝您的快速回復。第二,ButtonAnimation.this是什麼? – user2435575

+0

這是我的Activity類的名稱,我忘了將其更改爲MainActivity.this。等待我將編輯我的答案 – SKK

+0

沒關係。我知道了。我的問題是當地的按鈕mybutton1和mybutton2。並在LoadAnimation中,我只需要添加此當前活動(MainActivity.this) – user2435575