2013-12-17 67 views
34

我有动画的Android动画阿尔法

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator"> 
    <alpha 
     android:fromAlpha="0.2" 
     android:toAlpha="1.0" 
     android:duration="500"/> 
</set> 

和ImageView的

<ImageView 
    android:id="@+id/listViewIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/settings" 
    android:alpha="0.2"/> 

和代码:

final Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha); 
final ImageView iv = (ImageView) findViewById(R.id.listViewIcon); 
anim .setFillAfter(true); 
iv.startAnimation(anim); 

所以一开始我有阿尔法0.2,并在ImageView的结束我想要AlphaView的ImageView。但它不这样工作 - 当动画开始更多的Alpha添加和动画finis h with alpha 0.2

我必须改变以使图像从0.2-> 1变为动画?

我检查了不同的设置 - 我设置了android:alpha =“1.0”,从alpa =“1.0”toAlpha =“0.2”它像我期望的那样工作 - 从alpha 1到0.2。它看起来像从ImageView的阿尔法是从动画阿尔法乘...

+0

为防止视图在最后跳回到0.2 alpha,请使用'fillAfter'属性:http://developer.android.com/reference/android/view/animation/Animation.html#attr_android:fillAfter – Jave

+0

事实并非如此。阿尔法不会去1.当我从1-> 0.2动画,它工作正常,并保持在0.2(我使用填充后)。当我想从0.2到1进行动画时,它会消失到接近0并且达到0.2 – wioskamala

+0

您是否已将fillEnabled设置为true? – Jave

回答

64

试试这个

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f); 
animation1.setDuration(1000); 
animation1.setStartOffset(5000); 
animation1.setFillAfter(true); 
iv.startAnimation(animation1) 
+1

同样的结果就像从企业的性质imageview的在我的例子... – wioskamala

+0

reoveα和uninsatll应用从您的设备。清理,构建并重新安装。因为它在我的身边 –

+0

我不想删除阿尔法 - 图像应该与阿尔法0.2褪色。 Aplication做一些东西,并已准备好时,我想有我影像动画阿尔法1 – wioskamala

1
<ImageView 
    android:id="@+id/listViewIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/settings"/> 

从XML-> ImageView的删除android:apha=0.2

1

唔...

的事情是错误的,并可能在Android API在动画的正常运转。

的事实是,当你在0.2F的代码中的α值设定是基于Android的这意味着在XML文件中的设置是:

0.2f = 0.2f of 0.2f (20% from 100%) ie from 0.2f/5 = 0.04f 
1f = 0.2f 

所以你其实动画作品0.04 F到0.2F

标志setFillAfter(true)肯定的作品,但你要明白,你的动画ImageView年底将有Alpha值0.2F而不是一个,因为你指定0.2F为轻微ACC动画中的eptable值(一种最大的alpha通道)。

所以,如果你想有所需的结果应该把所有的逻辑运载到你的代码中,并在代码中操作动画而不是在xml中确定。

你应该明白,你的动画直接取决于两件事情:

的动画视图
  • 的LayoutParams
  • 动画参数。

动画参数操纵你的LayoutParams在setFillAfter \ setFillBefore方法。

+0

是顺利完美。你是对的。当我在我的代码中更改开始布局参数时,它就像一个魅力! – 2015-03-07 14:09:04

9

开始动画之前设置阿尔法1工作对我来说:

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f); 
animation1.setDuration(500); 
iv.setAlpha(1f); 
iv.startAnimation(animation1); 

至少在我的测试中,没有因为开始动画之前设置阿尔法闪烁。它工作正常。

+0

这也适用于我(编辑:我的错误,我有XML干扰:android:alpha =“0”)。会稍微玩一下,但我确实需要它从0开始,所以现在这个工作。谢谢。 –