我有一个图像,一旦加载页面就会淡入。但是,不会保留在动画中设置的图像的最终alpha。我的图像有以下(简单的)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);
那么,如何在动画完成后使图像的最终alpha保持不变呢?
感谢
在开始动画之前尝试添加.setFillAfter()
方法调用,如下所示:
myFadeInAnimation.setFillAfter(true);
body.startAnimation(myFadeInAnimation);
设置背景可绘制时,请尝试在ImageView setAlpha(float)
上设置alpha,该alpha应保留您最终设置动画的alpha值。
看起来我使用的是body.setBackgroundDrawable()而不是body.setImageDrawable(。当然,我还需要在其中添加setAlpha()。