繁体   English   中英

android-开始动画

[英]android - start animation

我想在用户做出滑动手势后开始播放动画。 我有一个设置动画的类和一个检测滑动的类。 我的问题是我不知道如何将它们组合在一起-我不希望我的动画在没有手势的情况下开始。 我是否需要在GestureDetector的if语句中启动动画方法? 以及如何从那里开始动画以备不时之需?

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;
                        }

您到底想在这里实现什么? 据我了解,您想为ImageView设置动画,对吗?

您可以在此处使用ViewPropertyAnimator而不是创建自定义类。

如果要移动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;
                    }

希望这段代码能给您带来启发!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM