在添加对象动画器来为 ImageView 设置动画之前,它运行良好。但是在创建动画后,它开始抛出运行时异常。尽管我知道是动画师造成的,但我无法弄清楚问题是什么,因为如果我删除了动画师初始化的代码,它就可以正常工作。请任何人帮我解决这个问题。谢谢!这是运行时异常的堆栈:
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2304
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2354
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 150
ActivityThread$H.handleMessage(Message) line: 1244
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5191
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 795
ZygoteInit.main(String[]) line: 562
NativeStart.main(String[]) line: not available [native method]
这是我的代码:
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Startup extends Activity {
private boolean itIsMultiplayerCard = false;
private float x, y;
private int screenWidth, screenHeight, initX, finX;
private Animation goDown, goUp;
private ObjectAnimator centerSlider;
private DisplayMetrics displayMetrics;
private FrameLayout startUpFrameLayout;
private ImageView backImageView, playNowImageView, multiplayerImageView, optionsImageView, exitImageView;
private OnClickListener backImageListener = new OnClickListener() {
@Override
public void onClick(View v) {
}
};
private OnClickListener playNowListener = new OnClickListener() {
@Override
public void onClick(View v) {
}
};
private OnClickListener multiplayerListener = new OnClickListener() {
@Override
public void onClick(View v) {
itIsMultiplayerCard = true;
optionsImageView.setOnClickListener(null);
exitImageView.setOnClickListener(null);
multiplayerImageView.setOnClickListener(backListener);
multiplayerImageView.startAnimation(goDown);
multiplayerImageView.setImageResource(R.drawable.blank);
multiplayerImageView.bringToFront();
multiplayerImageView.startAnimation(goUp);
}
};
private OnClickListener optionsListener = new OnClickListener() {
@Override
public void onClick(View v) {
itIsMultiplayerCard = false;
multiplayerImageView.setOnClickListener(null);
exitImageView.setOnClickListener(null);
optionsImageView.setOnClickListener(backListener);
optionsImageView.startAnimation(goDown);
optionsImageView.setImageResource(R.drawable.blank);
optionsImageView.bringToFront();
optionsImageView.startAnimation(goUp);
}
};
private OnClickListener exitListener = new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(0);
}
};
private OnClickListener backListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(itIsMultiplayerCard) {
optionsImageView.setOnClickListener(optionsListener);
multiplayerImageView.setOnClickListener(multiplayerListener);
multiplayerImageView.startAnimation(goDown);
multiplayerImageView.setImageResource(R.drawable.mul_u);
playNowImageView.bringToFront();
backImageView.bringToFront();
multiplayerImageView.startAnimation(goUp);
} else {
multiplayerImageView.setOnClickListener(multiplayerListener);
optionsImageView.setOnClickListener(optionsListener);
optionsImageView.startAnimation(goDown);
optionsImageView.setImageResource(R.drawable.op_u);
multiplayerImageView.bringToFront();
playNowImageView.bringToFront();
backImageView.bringToFront();
optionsImageView.startAnimation(goUp);
}
itIsMultiplayerCard = false;
exitImageView.setOnClickListener(exitListener);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenHeight = displayMetrics.heightPixels;
screenWidth = displayMetrics.widthPixels;
initX = backImageView.getWidth();
finX = ((screenWidth / 2) - (235 / 2));
goUp = AnimationUtils.loadAnimation(this, R.anim.go_up);
goDown = AnimationUtils.loadAnimation(this, R.anim.go_down);
startUpFrameLayout = (FrameLayout) findViewById(R.id.startUpFrameLayout);
backImageView = (ImageView) findViewById(R.id.backImageView);
playNowImageView = (ImageView) findViewById(R.id.playNowImageView);
multiplayerImageView = (ImageView) findViewById(R.id.multiplayerImageView);
optionsImageView = (ImageView) findViewById(R.id.optionsImageView);
exitImageView = (ImageView) findViewById(R.id.exitImageView);
centerSlider = ObjectAnimator.ofFloat(backImageView, "TranslationX", -117, 117);
backImageView.setOnClickListener(backImageListener);
playNowImageView.setOnClickListener(playNowListener);
multiplayerImageView.setOnClickListener(multiplayerListener);
optionsImageView.setOnClickListener(optionsListener);
exitImageView.setOnClickListener(exitListener);
playNowImageView.setAlpha(0.0f);
multiplayerImageView.setAlpha(0.0f);
optionsImageView.setAlpha(0.0f);
exitImageView.setAlpha(0.0f);
centerSlider.setDuration(300);
centerSlider.start();
}
}