Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 873
Fixpoint Avatar asked Oct 26 '10 08:10

Fixpoint


1 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.
like image 64
Fixpoint Avatar answered Sep 23 '22 02:09

Fixpoint