1

I want to create an animation for an image that is loaded into a vertical linear layout dynamically. When triggered I would like the image to slide down with its alpha set to 0 (this will slide all content below it down I hope) then fade the image in. Being new to Android animations I am having some trouble. Right now I'm trying to get just the fade in to work, here is what I have:

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>

<alpha
    xmlns:android="https://chemas.android.com/ap/res/android"
    android:interpolator="@android:anim/anticipate_interpolator"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="5000" >
</alpha>

In the Activity:

Animation animation = AnimationUtils.loadAnimation(context, R.anim.fade_in);

ImageView image = new ImageView(context);
image.setImageResource(R.drawable.my_image);
image.setLayoutParams(layoutParams);
image.setAlpha(0);

((ViewGroup) view).addView(image, 0);

image.startAnimation(animation);

The image is being loaded as all the context below it is shifted down, however if never fades in. I would like to get this working as well as having the image slide down so the dynamica add does not look so choppy.

Thanks in advance.

jjNford
  • 5,170
  • 7
  • 40
  • 64

1 Answers1

1

You have a typo in the xmlns:android tag.

It should be

xmlns:android = "http://schemas.android.com/apk/res/android"

With that being corrected, just remove the image.setAlpha(0); and it should animate. I am afraid I cannot give you a reason why it doesn't work with the alpha attribute set to 0.

Regarding the slide down animation, you will have to subclass Animation. Have a look at this answer that I think can help you get what you want: https://stackoverflow.com/a/5122460/871102

Community
  • 1
  • 1
Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • Thanks for the info - so the typo made the animation work, however instead of fading in over 5 seconds the the view waits 5 seconds then the alpha changes instantly from 0 to 100. Any cure for this? – jjNford Jun 06 '12 at 18:51
  • That is because of the interpolator. In your case I would use `android:interpolator="@android:anim/linear_interpolator"`. An interpolator defines how the animation changes through time. – Xavi Gil Jun 06 '12 at 21:36
  • Thx @Xavi, that fixed it, now to make it slide down. – jjNford Jun 07 '12 at 03:10