我正在开发一款游戏。它一切运作良好,我唯一需要的是当我按下手机上的主页或开/关按钮时我的动画暂停。就我记录正确而言,暂停脚本(找到here)效果很好。它不断增加时间并将其应用于动画。然而,当我恢复游戏时,动画都会在屏幕左侧恢复,而不是在我暂停时原来的位置。这是我的剥离代码。我真的希望有人可以帮助我。
public class GameView extends Activity{
ImageView peanut;
TranslateAnim movePeanut;
AnimationSet animSet = new AnimationSet(true);
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameviewlayout);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
movePeanut = new TranslateAnim(width,0,0,0);
movePeanut.setDuration(5000);
movePeanut.setRepeatCount(-1);
movePeanut.setRepeatMode(Animation.REVERSE);
animSet.addAnimation(movePeanut);
animSet.setFillAfter(true);
peanut = (ImageView) findViewById(R.id.imgview1);
peanut.startAnimation(animSet);
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
movePeanut.resume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
movePeanut.pause();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
public void finish (){
Intent intent = new Intent (GameView.this, MainActivity.class);
startActivity(intent);
}
public class TranslateAnim extends TranslateAnimation{
public TranslateAnim(float fromXDelta, float toXDelta, float fromYDelta,
float toYDelta) {
super(fromXDelta, toXDelta, fromYDelta, toYDelta);
// TODO Auto-generated constructor stub
}
private long mElapsedAtPause=0;
private boolean mPaused=false;
@Override
public boolean getTransformation(long currentTime, Transformation outTransformation) {
if(mPaused && mElapsedAtPause==0) {
mElapsedAtPause=currentTime-getStartTime();
}
if(mPaused)
setStartTime(currentTime-mElapsedAtPause);
boolean returnValue = super.getTransformation(currentTime, outTransformation);
return returnValue;
}
public void pause() {
mElapsedAtPause=0;
mPaused=true;
}
public void resume() {
mPaused=false;
}
}
}