Home » Android » android – How to position a view off-screen so that it can be Animated to move on-screen?

android – How to position a view off-screen so that it can be Animated to move on-screen?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a RelativeLayout filling the screen and a couple of ImageView positioned on it using LayoutParams margins. These ImageView are animated in different ways, and in one case I want an image to “fly in” to the screen from the right.

Unfortunately, if I set leftMargin for that ImageView greater than the width of the screen, it does not appear (or appears cropped if it partially visible at the start of animation).

I tried setting width and height of RelativeLayout to be bigger than screen size – it works, but only partially: if the image is positioned completely off-screen, it does not work, and if the image is partially visible, it is not cropped, but that works only for right and bottom sides.

So, my question is this: how to position several ImageViews on and off the screen so that I can animate them using Animation?

How to&Answers:

In the end, I used a trick: I combined AnimationDrawable and view animation. I did the following (assuming that the animation has to run T milliseconds):

  1. Position that ImageView on-screen.
  2. Set as its background an AnimationDrawable with following frames:
    • empty Drawable: 1ms,
    • normal Drawable: Tms.
  3. Change view’s animation to this:
    • jump to off-screen position (translation with duration=1ms),
    • do normal animation.

Answer:

This is probably a good place to start looking for those who would use Fixpoint’s solution.

Android — How to position View off-screen?