2016-11-23 32 views
0

我希望我的隨機選擇的圖像在動畫後重置,因此另一個隨機選擇的圖像位於原點,舊的圖像將被刪除。如何返回到動畫中設置圖像的部分?Android - 提取動畫

Random rand = new Random(); 
        int imag = rand.nextInt(4) + 1; 

        switch (imag) 
         { 
         case 1: 
          imageView.setImageResource(R.drawable.a); 

          break; 

         case 2: 
          imageView.setImageResource(R.drawable.b); 

          break; 

         case 3: 
          imageView.setImageResource(R.drawable.c); 

          break; 

         case 4: 
          imageView.setImageResource(R.drawable.d); 

          break; 
          }     

     }   


     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 (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1)) 
        { 

         animator.translationX(imageView.getWidth()*2) 
           .setDuration(180) //it's optional        
           .setListener(new AnimatorListenerAdapter() { 
            @Override 
            public void onAnimationEnd(Animator animation) { 
             super.onAnimationEnd(animation);            
            } 
           }) 

           .start(); 
        } 
        } 

回答

2

也許你可以嘗試這樣的事:

int getMyRandomResId() { 
    Random rand = new Random(); 
    int imag = rand.nextInt(4); 

    switch (imag) { 
     case 0: 
      return R.drawable.a; 
     break; 
     case 1: 
      return R.drawable.b; 
     break; 
     case 2: 
      return R.drawable.c; 
     break; 
     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) { 
       if (x1 > x2 && (x1 - x2) >= (y1 - y2) && (x1 - x2) >= (y2 - y1)) { 
        animationRunning = true; 
        animator.translationX(-imageView.getWidth() * 2) 
          .setDuration(180) 
          .setListener(new AnimatorListenerAdapter() { 
           @Override 
           public void onAnimationEnd(Animator animation) { 
            imageView.setTranslationX(0); 
            imageView.setImageResource(getMyRandomResId()); 
            animationRunning = false; 
           } 
          }) 
          .start(); 
       } 
      } 
     } 
    } 
} 
+0

我編輯的代碼以獲取更多信息。在對我的圖像進行動畫處理後,它應該放回原點並選擇一個新的圖像 –

+0

不確定這是否正是您想要的 - 最後可能會跳轉,因此可能需要額外的入口動畫。 –

+0

我試過了你的代碼,但我不知道你的'public void performAnimation()'方法是什麼,因爲它沒有被調用,所以我把它放了。你的代碼的另一部分'getMyRandomResId()'聽起來對我來說很合理,但如果我設置它,我會得到一個空白屏幕,這是什麼原因? –