• 9
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Using fade in animation for a view

I want to have a View that initially is invisible and when I press a button, it becomes visible with a fade in animation. I'm using the AlphaAnimation for the fading effect. The problem is that if I make the view invisible the animation can't be seen.

Thanks a lot,

Gratzi

Suppose you have an ImageView named imageView and an animation file your_fade_in_anim.xml inside your res\anim\ folder:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
// Now Set your animation
imageView.startAnimation(fadeInAnimation);

Your XML

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0.0"
        android:toAlpha="1.0" 
        android:interpolator="@android:anim/accelerate_interpolator" 
        android:duration="[duration (in milliseconds)]"
        android:repeatCount="infinite" />
</set>

Replace the brackets with your actual duration.

  • 126
Reply Report
      • 2
    • While it doesn't answer the question in the body, it answers the question of "How do I fade in a view", which is the title of the question. Because of that, I imagine a lot of people find this answer while looking for how to do animations (as I did). While not strictly the answer, it is definitely useful.

Provide an AnimationListener to the Animation and make the View visible as soon as the Animation starts.

http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

  • 26
Reply Report
    • Thanks a lot, man! The problem is that the application is not performed if the view is invisible, so the onStartAnimation method couldn't be used. But Ive set the view visible in the button's listener, before starting the animation, and I'm setting the view to be invisible in the onEndAnimation.
      • 2
    • I don't understand why you set the animation to invisible in onEndAnimation, in your initial question you say that you want it to fade in and become visible, why you set to invisible in onEndAnimation?

Instead of the infinite repeat count and hiding/viewing your View, I suggest to just not repeat the animation and initially start with the alpha channel set to maximum. Then you can use:

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0.0"
        android:toAlpha="1.0" 
        android:interpolator="@android:anim/accelerate_interpolator" 
        android:duration="[duration (in milliseconds)]"
        android:repeatCount="0" />
</set>

And you're done. No need for a Listener, hiding or showing. Just as simple.

  • 3
Reply Report