2016-11-21 2 views
1

Я хочу начать анимацию после того, как пользователь сделал жестов. У меня есть класс, где я устанавливаю анимацию и класс, который обнаруживает салфетки. Моя проблема в том, что я не знаю, как я их совмещаю - я не хочу, чтобы моя анимация начиналась без жестов. Мне нужно запустить метод анимации в if-statement GestureDetector? И как мне начать анимацию оттуда, если мне это нужно?android - start animation

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

спасибо. Является ли мой класс анимации не обязательным? –