2016-12-23 18 views
1

私のアプリでは、画像をランダムに選択して画面中央に配置したいと考えています。その後、アニメーションを実行することができます。その後、画像は元の位置に移動してリセットされます。次に、スイッチケースの新しいイメージを選択したいが、イメージは常に同じままである。どうすれば修正できますか?android - switch文を繰り返してください

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();   
         } 
      } 
      } 
      } 
     } 
} 
+0

が、私は 'getMyRandomResId()'の部分を失うことなく、方法でそれを置くことができホープ? –

+0

デバッガでコードをステップ実行します。これはおそらく、問題の根本的な原因を見つける最速の方法です。 –

+0

しかし、方法が見つからない場合、デバッガは何を言いますか? –

答えて

1
  int getMyRandomResId() { 

    int imag = rand.nextInt(4); 
       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; 
      } 
     } 

あなたはこののみ

のbcoz同じ画像を得た理由は、それが

+0

前の答えを申し訳ありません私の間違いは、これはあなたを確実に助ける!!!!!!!! –

+0

ありがとう、それは働いている –

関連する問題