我正在开发一款Android游戏卡片游戏,轮到四个玩家在那里转牌。一轮四张牌之后,我将检查谁赢了这一轮,本轮的获胜者将开始下一轮。现在我面临的问题是,我看不到第四页在中间安顿下来。一旦它结束最后交易卡的玩家的动画,它将调用该方法来检查获胜者并开始下一轮与获胜者玩家,因此每次最后一页开始动画转到中心并消失而不安定下来,并且呼吁下一轮。 这是我的代码
在onclick方法中,用户将选择一张卡并通过按下播放按钮,左,右或顶部按钮来处理它,并且runanime和play1都会启动动画 对于特定的卡
public void onClick(View v) {
Second sec = new Second();
third_turn three = new third_turn();
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String StartingPoint = "";
try {
StartingPoint = pref.getString("Start from", "");
} catch (Exception ex) {
Log.e(TAG, "exception on catch" + ex);
}
switch (v.getId()) {
case R.id.btn_sort_1:
String[] sorted = sort(arrPlayer1);
setup(sorted);
break;
case 0:
Bitmap bitmap = ((BitmapDrawable) imgViewArr[0].getDrawable())
.getBitmap();
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor.putString("SharedImage", encodetobase64(bitmap));
editor.putInt("cardno", 0);
editor.commit();
break;
case 1:
Bitmap bitmap2 = ((BitmapDrawable) imgViewArr[1].getDrawable())
.getBitmap();
SharedPreferences.Editor editor2 = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor2.putString("SharedImage", encodetobase64(bitmap2));
editor2.putInt("cardno", 1);
editor2.commit();
break;
case R.id.btn_play:
SharedPreferences pref1 = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor_pl = PreferenceManager
.getDefaultSharedPreferences(this).edit();
int cardno = pref1.getInt("cardno", 0);
if (!mp.isPlaying()) {
mp.start();
}
play1(cardno);
if (StartingPoint.equals("")) {
editor_pl.putInt("toset", 0);
temp[0] = arrPlayer1[cardno];
}
if (StartingPoint.equals("start from left")) {
editor_pl.putInt("toset", 3);
temp[3] = arrPlayer1[cardno];
}
if (StartingPoint.equals("start from top")) {
editor_pl.putInt("toset", 2);
temp[2] = arrPlayer1[cardno];
}
if (StartingPoint.equals("start from right")) {
editor_pl.putInt("toset", 1);
temp[1] = arrPlayer1[cardno];
}
editor_pl.commit();
if (arrPlayer1.length > 1) {
arrPlayer1 = update(arrPlayer1, cardno);
play.setEnabled(false);
setup(arrPlayer1);
}
Bottom.setFocusable(false);
break;
case R.id.card_left:
if (StartingPoint.equals("")) {
temp[1] = Second.bot1(arrPlayer2, temp[0], sir);
runanime(temp[1], left, 1);
arrPlayer2 = play(arrPlayer2, temp[1]);
} else if (StartingPoint.equals("start from top")) {
temp[3] = third_turn.bot3(arrPlayer2, temp[0], temp[1],
temp[2], sir);
runanime(temp[3], left, 3);
arrPlayer2 = play(arrPlayer2, temp[3]);
} else if (StartingPoint.equals("start from right")) {
temp[2] = third_turn.bot2(arrPlayer2, temp[0], temp[1], sir);
runanime(temp[2], left, 2);
arrPlayer2 = play(arrPlayer2, temp[2]);
}
left.setFocusable(false);
break;
case R.id.card_top:
if (StartingPoint.equals("")) {
temp[2] = third_turn.bot2(arrPlayer3, temp[0], temp[1], sir);
runanime(temp[2], top, 2);
arrPlayer3 = play(arrPlayer3, temp[2]);
} else if (StartingPoint.equals("start from left")) {
temp[1] = Second.bot1(arrPlayer3, temp[0], sir);
runanime(temp[1], top, 1);
arrPlayer3 = play(arrPlayer3, temp[1]);
} else if (StartingPoint.equals("start from right")) {
temp[3] = third_turn.bot3(arrPlayer3, temp[0], temp[1],
temp[2], sir);
runanime(temp[3], top, 3);
arrPlayer3 = play(arrPlayer3, temp[3]);
}
top.setFocusable(false);
break;
case R.id.card_right:
if (StartingPoint.equals("")) {
temp[3] = third_turn.bot3(arrPlayer4, temp[0], temp[1],
temp[2], sir);
runanime(temp[3], right, 3);
arrPlayer4 = play(arrPlayer4, temp[3]);
} else if (StartingPoint.equals("start from left")) {
temp[2] = third_turn.bot2(arrPlayer4, temp[0], temp[1], sir);
runanime(temp[2], right, 2);
arrPlayer4 = play(arrPlayer4, temp[2]);
} else if (StartingPoint.equals("start from top")) {
temp[1] = Second.bot1(arrPlayer4, temp[0], sir);
runanime(temp[1], right, 1);
arrPlayer4 = play(arrPlayer4, temp[1]);
}
right.setFocusable(false);
break;
}
}
runanime方法:根据字符串,我将知道我必须用于动画的卡片。最后一个参数是知道在中间设置该卡的位置。
private void runanime(String card, ImageView from, int to) {
Bitmap bmp = ((BitmapDrawable) from.getDrawable()).getBitmap();
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor.putString("SharedImage", encodetobase64(bmp));
editor.putInt("toset", to);
editor.commit();
ViewParent view = from.getParent();
sp.play(soundIds[0], 1, 1, 1, 0, 1);
((View) view).startAnimation(animMove);
}
private void runanime(String card,ImageView from,int to){
Bitmap bmp = ((BitmapDrawable) from.getDrawable()).getBitmap();
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor.putString("SharedImage", encodetobase64(bmp));
editor.putInt("toset", to);
editor.commit();
ViewParent view = from.getParent();
sp.play(soundIds[0], 1, 1, 1, 0, 1);
((View) view).startAnimation(animMove);
}
现在在OnAnimationEnd方法中,我将检索我在共享首选项中设置的值并在中间设置图像,并将tham存储为临时数组中的字符串以了解获胜者。 public void onAnimationEnd(Animation arg0){
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
Bitmap bmp = decodetobase64(pref.getString("SharedImage", ""));
Drawable image = new BitmapDrawable(getResources(), bmp);
int toset = pref.getInt("toset", 0);
//String StartingPoint = pref.getString("Start from", "");
switch (toset) {
case 0:
mid_1.setImageDrawable(image);
flag1 = true;
top.setFocusable(true);
top.performClick();
break;
case 1:
mid_2.setImageDrawable(image);
flag2 = true;
right.setFocusable(true);
right.performClick();
break;
case 2:
mid_3.setImageDrawable(image);
flag3 = true;
Bottom.setFocusable(true);
play.setEnabled(true);
break;
case 3:
mid_4.setImageDrawable(image);
flag4 = true;
break;
}
if (flag1 && flag2 && flag3 && flag4) {
NextPlayer(temp);
}
}
在设置mid_4之后,flag4设置为true并且if循环开始执行,但在视觉上我看不到第四张卡安定下来。即使我试图使用thread.sleep方法,但它不会派上用场。所以请任何人告诉我该怎么做? 看起来在OnAnimationEnd方法中,flag4在图像设置为mid_4之前变为true。