AlphaAnimation 不起作用 [英] AlphaAnimation does not work

查看:37
本文介绍了AlphaAnimation 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决问题的方法.但我的代码似乎没问题.

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

我会试着解释一下:我的布局定义中有一个带有 android:alpha="0" 的 TextView.我想(当单击图像时)显示带有 AlphaAnimation 的 TextView,从 0.0f 到 1.0f.

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.

我的问题是,当我点击图片时,没有任何反应.但奇怪的是,如果我在布局定义中将它的 alpha 设置为 1,然后单击图像,我可以看到动画(alpha 1 -> alpha 0 -> alpha 1).

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

我做错了什么?

我的代码:

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

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

提前致谢.

推荐答案

问题出在 android:alpha="0".此属性设置视图的透明度 http://developer.android.com/reference/android/view/View.html#attr_android:alpha

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

当 alpha 属性等于 0 时,动画将透明度从 0*0.0f=0 更改为 0*1.0f=0.当 alpha 属性设置为 1 时,动画将透明度从 1*0.0f=0 更改为 1*1.0f=1.这就是为什么在第一种情况下您看不到文本,而在第二种情况下一切正常.

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.

为了使事情正常工作,您必须在布局 xml 中将可见性属性设置为不可见.在开始 alpha 动画之前调用 tv.setVisibility(View.VISIBLE);

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

这篇关于AlphaAnimation 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆