我想擁有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>
我希望動畫爲每個按鈕單獨控制。
嘗試改變findViewById(R.id.button1).startAnimation(抖動); to myButton.startAnimation(shake); – npace
我試過了,但它不起作用,它不能先解決 – user2435575