图像淡入效果无法保持最终透明度

4

我有一张图片,在页面加载后会淡入。然而,动画中设置的图像最终透明度并没有保留。以下是我图片的(简单)XML代码:

    <ImageView 
    android:id="@+id/myImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cropToPadding="true"
    android:scaleType="centerCrop"
    android:background="#ffffff" />

然后我有一个动画文件,它淡入了图片:

    <set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha 
    android:fromAlpha="0.0" 
    android:toAlpha="0.6"  
    android:duration="2000"/> 
    </set> 

最后是加载图片的代码:

    body =(ImageView)findViewById(R.id.myImage);
    body.setBackgroundDrawable(new BitmapDrawable(this.getResources(), background));
    Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein_bg);
    body.startAnimation(myFadeInAnimation);

那么,动画完成后我如何使图像的最终阿尔法值保持不变呢?谢谢。
3个回答

4
尝试在动画开始之前添加.setFillAfter()方法调用,如下所示:
myFadeInAnimation.setFillAfter(true);
body.startAnimation(myFadeInAnimation);

1

当您设置背景可绘制时,请尝试在ImageView setAlpha(float) 上设置alpha值,这样应该会保留您最终要进行动画处理的alpha值。


我尝试过了,但仍然没有起作用。我在动画之前添加了body.setAlpha(20);,但在动画结束时它仍然变成了完整的颜色。 - Nick

1

看起来我使用的是body.setBackgroundDrawable()而不是body.setImageDrawable()。当然,我还需要在其中添加setAlpha()。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接