I want to have to buttons with 2 different XML animation files.
I tried this code first:
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);
}
}
and i changed it to this
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;
}
}
this is vanish.xml animation file
<?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>
this is rshake.xml animation file
<?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>
this is cycleby.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="10" />
and this is the layout file 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>
I want the animation to work separately for each button.