アプリ作成中です。
画面左側はアニメーションが動く。
画面右側は操作・問題画面。計6つ(上・下・左・右・スタート・やり直し)のボタンで操作する。
<実現したいこと>
(X == -2 && Y ==1)の位置へ到達したらアニメーションを一時停止する。その際、表示されたダイアログの「OK」ボタンを押したら一時停止した位置からアニメーションを再開する
<試してみたこと>
①(X == -2 && Y ==1)の位置へ到達したら
java
1if(x == -2 && y == 1){ 2 ReplayFlag = false; 3 } 4 5```と記述し、アニメーションを一時停止させた 6 7 8②表示されたダイアログの「OK」ボタンを押したら 9```java 10dl.setPositiveButton("OK", new DialogInterface.OnClickListener() { 11 @Override 12 public void onClick(DialogInterface dialog, int which) { 13 ReplayFlag = true; 14 animationReplay(); 15 16 17```と記述し、アニメーションを再開させようとしたが、一時停止した位置からではなく初期位置からのアニメーションになってしまった 18 19 20 21 22```java 23public class MainActivity extends AppCompatActivity implements Animator.AnimatorListener{ 24 final static float TRANSLATE_X = 240.0f; // 1回の操作でX軸方向にいくつ動かすか 25 final static float TRANSLATE_Y = 245.0f; // 1回の操作でY軸方向にいくつ動かすか 26 27 // 盤面上の開始座標を初期値として設定する 28 private int x = 0; 29 private int y = 0; 30 private String text; 31 private ArrayList<Integer> History; // 履歴を格納するリスト 32 private boolean ReplayFlag; // 再生中であることを示すフラグ 33 private int ReplayCount; // 再生経過のカウンター 34 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 41 Button btn1 = (Button) findViewById(R.id.button1); // うえ 42 Button btn2 = (Button) findViewById(R.id.button2); // した 43 Button btn3 = (Button) findViewById(R.id.button3); // ひだり 44 Button btn4 = (Button) findViewById(R.id.button4); // みぎ 45 Button btn5 = (Button) findViewById(R.id.button5); // やりなおし 46 Button btn6 = (Button) findViewById(R.id.button6); // スタート 47 48 text = ""; 49 final TextView result = (TextView) findViewById(R.id.result); 50 result.setText(""); 51 52 // 初期値をセット 53 History = new ArrayList<>(); 54 ReplayFlag = false; 55 ReplayCount = 0; 56 57 58 btn1.setOnClickListener(new View.OnClickListener() { 59 @Override 60 public void onClick(View v) { 61 // 「うえ」を押したときの処理 62 TextView result = (TextView) findViewById(R.id.result); 63 text += "<font color=#98fb98>↑</font>"; 64 result.setText(Html.fromHtml(text)); 65 History.add(0); // 0は上を意味する 66 } 67 }); 68 btn2.setOnClickListener(new View.OnClickListener() { 69 @Override 70 public void onClick(View v) { 71 // 「した」を押したときの処理 72 TextView result = (TextView) findViewById(R.id.result); 73 text += "<font color=#fffacd>↓</font>"; 74 result.setText(Html.fromHtml(text)); 75 History.add(1); // 1は下を意味する 76 } 77 }); 78 btn3.setOnClickListener(new View.OnClickListener() { 79 @Override 80 public void onClick(View v) { 81 // 「ひだり」を押したときの処理 82 TextView result = (TextView) findViewById(R.id.result); 83 text += "<font color=#ffc0cb>←</font>"; 84 result.setText(Html.fromHtml(text)); 85 History.add(2); // 2は左を意味する 86 } 87 }); 88 btn4.setOnClickListener(new View.OnClickListener() { 89 @Override 90 public void onClick(View v) { 91 // 「みぎ」を押したときの処理 92 TextView result = (TextView) findViewById(R.id.result); 93 text += "<font color=#afeeee>→</font>"; 94 result.setText(Html.fromHtml(text)); 95 History.add(3); // 3は右を意味する 96 } 97 }); 98 btn5.setOnClickListener(new View.OnClickListener() { 99 @Override 100 public void onClick(View v) { 101 x = 0; 102 y = 0; 103 animationImageReset(); 104 History.clear(); 105 106 text = ""; 107 TextView result = (TextView) findViewById(R.id.result); 108 result.setText(""); 109 110 imageReset(); 111 } 112 }); 113 btn6.setOnClickListener(new View.OnClickListener() { 114 @Override 115 public void onClick(View v) { 116 // isEmpty()でリストが空っぽではない(1手以上の記録がある)か調べる 117 if (!History.isEmpty()) { 118 // 猫を最初の位置に戻す 119 x = 0; 120 y = 0; 121 animationImageReset(); 122 123 ReplayFlag = true; // 再生中にする 124 animationReplay(); // 再生する(ここでは1手目だけ、以後はonAnimationEndに任せる) 125 126 } 127 }); 128 } 129 130 private void imageReset(){ 131 ImageView imageView = (ImageView) findViewById(R.id.imageView); 132 imageView.setVisibility(View.INVISIBLE); 133 } 134 135 private void animationImageView(String property, float start, float end) { 136 ImageView imageView = (ImageView) findViewById(R.id.MyCat); 137 ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, property, start, end); 138 animator.setDuration(1000); 139 animator.addListener(this); 140 animator.start(); 141 } 142 143 private void animationImageReset() { 144 final ImageView MyCat = (ImageView) findViewById(R.id.MyCat); 145 ObjectAnimator.ofFloat(MyCat, "translationX", 0.0f, 0.0f).start(); 146 ObjectAnimator.ofFloat(MyCat, "translationY", 0.0f, 0.0f).start(); 147 } 148 149 private void Reset(){ 150 x = 0; 151 y = 0; 152 animationImageReset(); 153 History.clear(); 154 155 text = ""; 156 TextView result = (TextView) findViewById(R.id.result); 157 result.setText(""); 158 159 imageReset(); 160 } 161 162 private void encountDog() { 163 if (x == -2 && y == 1) { 164 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 165 dl.setTitle("エサを手に入れた!!"); 166 dl.setPositiveButton("OK", new DialogInterface.OnClickListener() { 167 @Override 168 public void onClick(DialogInterface dialog, int which) { 169 ReplayFlag = true; 170 animationReplay(); 171 ImageView imageView = (ImageView) findViewById(R.id.imageView); 172 imageView.setVisibility(View.VISIBLE); 173 } 174 }); 175 dl.show(); 176 } 177 } 178 private void animationReplay() { 179 if (ReplayFlag) { 180 int dir = History.get(ReplayCount); // リストから取り出し 181 182 switch (dir) { 183 case 0: // 0は上を意味する 184 upAnimation(); 185 break; 186 case 1: // 1は下を意味する 187 downAnimation(); 188 break; 189 case 2: // 2は左を意味する 190 leftAnimation(); 191 break; 192 case 3: // 3は右を意味する 193 rightAnimation(); 194 break; 195 } 196 197 ReplayCount++; // カウンターを増やす 198 199 // 再生終了したらフラグをクリアする 200 if (ReplayCount == History.size()) { 201 ReplayFlag = false; 202 ReplayCount = 0; 203 } 204 if(x == -2 && y == 1){ 205 ReplayFlag = false; 206 } 207 } 208 } 209 @Override 210 public void onAnimationStart(Animator animation) { 211 } 212 213 @Override 214 public void onAnimationEnd(Animator animation) { 215 // アニメーションが終了するとここが実行される 216 if (ReplayFlag) { 217 animationReplay(); // 再生する 218 } 219 } 220 221 @Override 222 public void onAnimationCancel(Animator animation) { 223 } 224 225 @Override 226 public void onAnimationRepeat(Animator animation) { 227 } 228 229 private void upAnimation(){ 230 // 「うえ」を押したときの処理 231 if (y > 0) { 232 float start = TRANSLATE_Y * y; 233 float end = start - TRANSLATE_Y; 234 animationImageView("translationY", start, end); 235 y--; 236 encountDog(); 237 } 238 } 239 private void downAnimation(){ 240 // 「した」を押したときの処理 241 if (y < 3) { 242 float start = TRANSLATE_Y * y; 243 float end = start + TRANSLATE_Y; 244 animationImageView("translationY", start, end); 245 y++; 246 encountDog(); 247 } 248 } 249 private void leftAnimation(){ 250 // 「ひだり」を押したときの処理 251 if (x > -3) { 252 float start = TRANSLATE_X * x; 253 float end = start - TRANSLATE_X; 254 animationImageView("translationX", start, end); 255 x--; 256 encountDog(); 257 } 258 } 259 private void rightAnimation(){ 260 // 「みぎ」を押したときの処理 261 if (x < 0) { 262 float start = TRANSLATE_X * x; 263 float end = start + TRANSLATE_X; 264 animationImageView("translationX", start, end); 265 x++; 266 encountDog(); 267 } 268 } 269}
回答1件
あなたの回答
tips
プレビュー