37

I have been looking for a solution to my problem. But my code seems to be ok.

I'll try to explain: I have a TextView with android:alpha="0" in my layout definition. I want (when a image is clicked) show that TextView with an AlphaAnimation, from 0.0f to 1.0f.

My problem is that when I click the image, nothing happens. But the strange thing, is that if I set it's alpha to 1 in the layout definition, and I click the image, I can see the animation (alpha 1 -> alpha 0 -> alpha 1).

What am I doing wrong?

My code:

TextView tv = (TextView) findViewById(R.id.number);

AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setFillAfter(true);
tv.startAnimation(animation1);

Thanks in advance.

jjimenez
  • 853
  • 1
  • 7
  • 18

4 Answers4

84

The problem is in android:alpha="0". This property sets transparency of a View http://developer.android.com/reference/android/view/View.html#attr_android:alpha

When alpha property is equal to 0 then animation is changing transparency from 0*0.0f=0 to 0*1.0f=0. When alpha property is set to 1 then animation is changing transparency from 1*0.0f=0 to 1*1.0f=1. That's why in first case you can't see text and in the second everything works as expected.

To make things work you have to set visibility property to invisible in layout xml. And before starting alpha animation call tv.setVisibility(View.VISIBLE);

vasart
  • 6,692
  • 38
  • 39
  • Thanks for the response vasart. I will try it when I will arrive to home. But, I think that doing that, before the animation start I will see the TextView element will appear whith alpha 1, and then it will start its animation. Is that right? What I want to do is that the TextView appears slowly in my view. – jjimenez Jul 09 '12 at 07:02
  • It should appear slowly without flickering at start – vasart Jul 09 '12 at 08:09
  • I seems a partial response, what happen if i set android:visibility="INVISIBLE or GONE", the animation to alpha 1 doesnt work. looks like property animator need the view. – AXSM Jan 13 '15 at 06:06
  • Use `Invisible` instead of `Gone` was what did the trick. Gone == Alpha 0 – Pierre May 27 '17 at 16:22
19

More simple way is presented in this answer:

tv.animate().alpha(1).setDuration(1000);
Community
  • 1
  • 1
ULazdins
  • 1,975
  • 4
  • 25
  • 31
0

actually, android have TWO alpha property for a view

    /**
     * The opacity of the View. This is a value from 0 to 1, where 0 means
     * completely transparent and 1 means completely opaque.
     */
    @ViewDebug.ExportedProperty
    float mAlpha = 1f;

    /**
     * The opacity of the view as manipulated by the Fade transition. This is a hidden
     * property only used by transitions, which is composited with the other alpha
     * values to calculate the final visual alpha value.
     */
    float mTransitionAlpha = 1f;


/**
 * Calculates the visual alpha of this view, which is a combination of the actual
 * alpha value and the transitionAlpha value (if set).
 */
private float getFinalAlpha() {
    if (mTransformationInfo != null) {
        return mTransformationInfo.mAlpha * mTransformationInfo.mTransitionAlpha;
    }
    return 1;
}

the view final alpha is the product of the TWO alpha

View#setAlpha(float) & View#animate() & android:alpha -> mAlpha

AlphaAnimation -> mTransitionAlpha

Yessy
  • 1,172
  • 1
  • 8
  • 13
0

Setting the fillBefore attribute of animation to true fixed the issue for me.

TextView tv = (TextView) findViewById(R.id.number);

AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setFillBefore(true);
tv.startAnimation(animation1);

FillBefore sets the transformation prior to starting animation.

user14678216
  • 2,886
  • 2
  • 16
  • 37