Search code examples
androidimageanimationscreen-size

Doing Translate TranslateAnimation from just outside the screen on one side to just outside the screen on the other side


I have a ImageView called myimage.

I'm doing it TranslateAnimation from just outside the screen on one side to just outside the screen on the other side.

Animation animation = new TranslateAnimation(0, 0, -1500, 1500);
animation.setDuration(1000);
animation.setFillAfter(false);
myimage.startAnimation(animation);

Is there any way that lets me move it from just outside the screen on one side to just outside the screen on the other side regardless of any screen size ?


Solution

  • I have got it now.

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    final float screen_width_half = metrics.widthPixels/2; 
    final float distance = screen_width_half + 50;
    
    //determine half screen width and add 50 to it to take it just a little outside the screen
    
    Animation animation = 
    new TranslateAnimation(0, 0, -distance, distance);
    animation.setDuration(1000);
    animation.setFillAfter(false);
    myimage.startAnimation(animation);