在我的應用程序中,我想隨機選擇一個圖像,並將其放在屏幕的中心。之後,可以執行動畫,之後圖像將被移動並重置爲其原始位置。然後,我想選擇一個新的開關盒圖像,但圖像始終保持不變。我該如何解決它?android - 重複開關語句
public class MainActivity extends Activity implements OnGestureListener
{
private ImageView imageView;
float x1,x2;
float y1, y2;
public int sco = 0;
TextView score;
final Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageview1);
imageView.setImageResource(getMyRandomResId());
score = (TextView)findViewById(R.id.textView2);
}
int imag = rand.nextInt(4);
int getMyRandomResId() {
switch (imag) {
case 0:
return R.drawable.a;
case 1:
return R.drawable.b;
case 2:
return R.drawable.c;
default:
return R.drawable.d;
}
}
private boolean animationRunning = false;
public boolean onTouchEvent(MotionEvent touchevent)
{
final ViewPropertyAnimator animator = imageView.animate();
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
if (!animationRunning) {
//left to right sweep event on screen
if (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1)) {
animationRunning = true;
animator.translationX(imageView.getWidth() * 2)
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if(getMyRandomResId() == R.drawable.ballgrue) {
sco++;
}
imageView.setTranslationX(0);
imageView.setImageResource(getMyRandomResId());
animationRunning = false;
}
})
.start();
}
}
}
}
}
}
我可以把它放在一個方法沒有鬆動的'getMyRandomResId()'一部分? –
使用調試器瀏覽代碼。這可能是找到問題根源的最快方法。 –
但是,如果缺少一個方法,調試器會說什麼? –