方法
使用补间视图动画。
预期结果
一个按钮,放置在原地A
,一旦点击,它就会向右移动到 spot B
。然后我当场点击那个按钮B
,它向左移动到spot A
。
问题
首先点击spot A
,按钮确实移动到spot B
。然后点击点B
没有响应,相反,它总是响应点击点A
。
主.java
package com.example.animationdemo;
import android.app.Activity;
import android.os.Bundle;
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 Main extends Activity {
/** Called when the activity is first created. */
boolean flagToggleButton = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener () {
@Override
public void onClick(View arg0) {
if (flagToggleButton == false) {
Animation anim = AnimationUtils.loadAnimation(Main.this, R.anim.animation_move_right);
b.startAnimation(anim);
flagToggleButton = true;
}
else {
Animation anim = AnimationUtils.loadAnimation(Main.this, R.anim.animation_move_left);
b.startAnimation(anim);
flagToggleButton = false;
}
}
});
}
}
anim_move_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillEnabled="true"
android:fillAfter="true" >
<translate
android:duration="1000"
android:fromXDelta="0%"
android:toXDelta="200%"
android:startOffset="0" />
</set>
anim_move_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillEnabled="true"
android:fillAfter="true" >
<translate
android:duration="1000"
android:fromXDelta="0%"
android:toXDelta="-200%"
android:startOffset="0" />
</set>