Nick
Nick

Reputation: 6385

Image fade-in doesnt keep final alpha

I have an image that fades in once the page is loaded. However the final alpha of the image that is set in the animation is not kept. I have the following (simple) xml for my image:

    <ImageView 
    android:id="@+id/myImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cropToPadding="true"
    android:scaleType="centerCrop"
    android:background="#ffffff" />

Then I have the animation file which fades in the image:

    <set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha 
    android:fromAlpha="0.0" 
    android:toAlpha="0.6"  
    android:duration="2000"/> 
    </set> 

Then finally the code which loads the image:

    body =(ImageView)findViewById(R.id.myImage);
    body.setBackgroundDrawable(new BitmapDrawable(this.getResources(), background));
    Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein_bg);
    body.startAnimation(myFadeInAnimation);

So how can I get the final alpha of the image to stay after the animation is complete?

Thanks

Upvotes: 4

Views: 535

Answers (3)

FoamyGuy
FoamyGuy

Reputation: 46856

Try adding the .setFillAfter() method call before you you start the animation like this:

myFadeInAnimation.setFillAfter(true);
body.startAnimation(myFadeInAnimation);

Upvotes: 4

Nick
Nick

Reputation: 6385

Looks like I was using body.setBackgroundDrawable() instead of body.setImageDrawable(). Of course I still need to add setAlpha() in there as well.

Upvotes: 1

jlindenbaum
jlindenbaum

Reputation: 1881

When you set the background drawable, try setting the alpha on the ImageView setAlpha(float) that should retain the alpha value that you're eventually animating to.

Upvotes: 1

Related Questions