Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

android - AlphaAnimation does not work

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...