Search code examples
positionandroid-animationcollision-detectionmovethread-sleep

Move view from top to bottom continuously without animation in Android


I want to know how to move a view from top to bottom continuously without animation. I am asking this because I want to get the position of the view at every step so that I can check that if there is any collision between that view and any other view.

With animation you can move (not exactly move) a view from one position to another position (Animator class), but animation produces an illusion to the user that it is moving but it's position is fixed all the time. So this can't be done using animation?

Second approach is incrementing position of view. I applied this method in onCreate(). If I used it without Thread.sleep(50) then the activity doesn't show the view, if I applied it with Thread.sleep(50) then activity doesn't start for some period.


Solution

  • Solved the problem using ValueAnimator :-

    CodeSnippet :-

    va=ValueAnimator.ofFloat(0.0f,size.y);
    va.setDuration(5000);
    va.setRepeatCount(va.INFINITE);
    va.setRepeatMode(va.REVERSE);
    va.start();
    va.addUpdateListener(new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // TODO Auto-generated method stub
                    bullet[0].setTranslationY((Float) va.getAnimatedValue());           
                       Rect R11=new Rect(bullet[0].getLeft(),bullet[0].getTop()+(int)bullet[0].getTranslationY(),bullet[0].getRight(),bullet[0].getBottom()+(int)bullet[0].getTranslationY());
                          Rect R21=new Rect(ball.getLeft(), ball.getTop(), ball.getRight(), ball.getBottom());
                          if(R11.intersect(R21))
                            va.cancel();
        }
    });