2016-11-21 98 views
1

我想在用戶做一個滑動手勢後開始動畫。我有一個課,我設置了動畫和一個正在檢測滑動的類。我的問題是,我不知道如何將它們結合起來 - 我不希望我的動畫在沒有手勢的情況下開始。我是否需要在GestureDetector的if語句中啓動動畫方法?如果我需要這樣做,我該如何從那裏開始動畫?android-開始動畫

public class MainActivity extends Activity implements OnGestureListener 
{ 
      private ImageView imageView; 
      private BitmapDrawable ball; 
      float x1,x2; 
      float y1, y2; 
      Context mContext = getApplicationContext(); 
      @Override 
      protected void onCreate(Bundle savedInstanceState) 
      {      
         super.onCreate(savedInstanceState);      
         setContentView(R.layout.activity_main);       
         imageView = (ImageView) findViewById(R.id.imageview1); 

      }   



         public boolean onTouchEvent(MotionEvent touchevent) 
         { 
            switch (touchevent.getAction()) 
            { 
              // when user first touches the screen we get x and y coordinate 
              case MotionEvent.ACTION_DOWN: 
              { 
               x1 = touchevent.getX(); 
               y1 = touchevent.getY(); 
               break; 
              } 
              case MotionEvent.ACTION_UP: 
              { 
               x2 = touchevent.getX(); 
               y2 = touchevent.getY(); 

               //if left to right sweep event on screen 
               if (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1)) 
               {  
                imageView.animate() 
                 .translationX(-imageView.getWidth()) //in this case Image goes to the left 
                 .setDuration(180) //it's optional 
                 .setListener(new AnimatorListenerAdapter() { 
                   @Override 
                   public void onAnimationEnd(Animator animation) { 
                    super.onAnimationEnd(animation); 
                   } 
                }) 
                .start(); 

                Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();            
                } 

               // if right to left sweep event on screen 
               if (x1 > x2 && (x1-x2)>=(y1-y2) && (x1-x2)>=(y2-y1)) 
               { 
                imageView.animate() 
                 .translationX(imageView.getWidth()) //in this case Image goes to the right 
                 .setDuration(180) //it's optional 
                 .setListener(new AnimatorListenerAdapter() { 
                   @Override 
                   public void onAnimationEnd(Animator animation) { 
                    super.onAnimationEnd(animation); 
                   } 
                }) 
                .start(); 
                Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show(); 
               } 

               // if UP to Down sweep event on screen 
               if (y1 < y2 && (y2-y1)>=(x1-x2) && (y2-y1)>=(x2-x1)) 
               { 
                Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show(); 
               } 

               //if Down to UP sweep event on screen 
               if (y1 > y2 && (y1-y2)>=(x1-x2) && (y1-y2)>=(x2-x1)) 
               { 
                Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show(); 
                } 
               break; 
              } 
            } 
            return false; 
         } 

回答

0

你究竟想在這裏實現什麼?據我瞭解,你想動畫ImageView,對吧?

您可以在此處使用ViewPropertyAnimator而不是創建自定義類。

+0

ViewPropertyAnimator?我可以將它應用到檢測類中嗎?是的,我想通過刷動畫動畫 –

+0

你想實現什麼樣的動畫? – nullbyte

+0

我認爲這是一個ViewAnimation。動畫工作時,圖像從右側中心移動,但動畫從我開始應用程序開始,因爲我不知道如何將它連接到GestureDetector,因此動畫正在等待滑動 –

0

如果您想要移動您的ImageView根據用戶滑動的種類在您的GestureDetector實施中添加此代碼。

imageView.animate() 
     .translationX(-imageView.getWidth()) //in this case Image goes to the left 
     .setDuration(180) //it's optional 
     .setListener(new AnimatorListenerAdapter() { 
         @Override 
         public void onAnimationEnd(Animator animation) { 
          super.onAnimationEnd(animation); 
          //insert some code here if you want to make some changes to the image instance when animation is finished 

         } 
        }) 
      .start(); 

你可以在這裏添加此代碼,例如:

public boolean onTouchEvent(MotionEvent touchevent) 
        { 
           switch (touchevent.getAction()) 
           { 
             // when user first touches the screen we get x and y coordinate 
             case MotionEvent.ACTION_DOWN: 
             { 
              x1 = touchevent.getX(); 
              y1 = touchevent.getY(); 
              break; 
             } 
             case MotionEvent.ACTION_UP: 
             { 
              x2 = touchevent.getX(); 
              y2 = touchevent.getY(); 

              //if left to right sweep event on screen 
              if (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1)) 
              {  
               imageView.animate() 
                .translationX(-imageView.getWidth()) //in this case Image goes to the left 
                .setDuration(180) //it's optional 
                .setListener(new AnimatorListenerAdapter() { 
                  @Override 
                  public void onAnimationEnd(Animator animation) { 
                   super.onAnimationEnd(animation); 
                  } 
               }) 
               .start(); 

               Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();            
               } 

              // if right to left sweep event on screen 
              if (x1 > x2 && (x1-x2)>=(y1-y2) && (x1-x2)>=(y2-y1)) 
              { 
               imageView.animate() 
                .translationX(imageView.getWidth()) //in this case Image goes to the right 
                .setDuration(180) //it's optional 
                .setListener(new AnimatorListenerAdapter() { 
                  @Override 
                  public void onAnimationEnd(Animator animation) { 
                   super.onAnimationEnd(animation); 
                  } 
               }) 
               .start(); 
               Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show(); 
              } 

              // if UP to Down sweep event on screen 
              if (y1 < y2 && (y2-y1)>=(x1-x2) && (y2-y1)>=(x2-x1)) 
              { 
               Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show(); 
              } 

              //if Down to UP sweep event on screen 
              if (y1 > y2 && (y1-y2)>=(x1-x2) && (y1-y2)>=(x2-x1)) 
              { 
               Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show(); 
               } 
              break; 
             } 
           } 
           return false; 
        } 

我希望這段代碼給你的想法!

+0

目前爲止看起來不錯,但我在哪裏添加它?在動畫課上?我很新,所以我有問題設置它,對不起, –

+0

你已經得到了答案,我希望。 – nullbyte

+0

非常感謝。我的動畫課不再需要嗎? –