下面的代码为按钮添加了Scale动画。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.scale);
Button btnScale1 = findViewById(R.id.button1);
btnScale1.setOnClickListener(view -> view.startAnimation(animScale));
播放完动画后,我需要切换到另一个活动。我试图构建如下逻辑:
animScale.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p1.class);
startActivity(intent);
}
安卓工作室在新的Animation.AnimationListener((行中输出实现方法
完整代码:
package en.my.voicechat;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//implementation of animation for each button
final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.scale);
Button btnScale1 = findViewById(R.id.button1);
btnScale1.setOnClickListener(view -> view.startAnimation(animScale));
Button btnScale2 = findViewById(R.id.button2);
btnScale2.setOnClickListener(view -> view.startAnimation(animScale));
Button btnScale3 = findViewById(R.id.button3);
btnScale3.setOnClickListener(view -> view.startAnimation(animScale));
Button btnScale4 = findViewById(R.id.button4);
btnScale4.setOnClickListener(view -> view.startAnimation(animScale));
Button btnScale5 = findViewById(R.id.button5);
btnScale5.setOnClickListener(view -> view.startAnimation(animScale));
Button btnScale6 = findViewById(R.id.button6);
btnScale6.setOnClickListener(view -> view.startAnimation(animScale));
Button btnScale7 = findViewById(R.id.button7);
btnScale7.setOnClickListener(view -> view.startAnimation(animScale));
//attempt to apply the transition to Activity for the seventh button
animScale.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p7.class);
startActivity(intent);
}
});
}
}
您可能没有覆盖其他接口方法?(onAnimationStart,onAnimationRepeat(
不太确定,如果您将光标移到代码上方,它应该说明问题所在。
像这样:
animScale.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p7.class);
startActivity(intent);
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});