2016-12-23 69 views
1

在我的应用程序中,我想随机选择一个图像,并将其放在屏幕的中心。之后,可以执行动画,之后图像将被移动并重置为其原始位置。然后,我想选择一个新的开关盒图像,但图像始终保持不变。我该如何解决它?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();   
         } 
      } 
      } 
      } 
     } 
} 
+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

谢谢,它的工作 –