2016-11-21 125 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

非常感谢。我的动画课不再需要吗? –