Android动画Alpha

wio*_*ala 59 java animation android alpha kotlin

我有动画:

<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>
Run Code Online (Sandbox Code Playgroud)

并且ImageView:

<ImageView
    android:id="@+id/listViewIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/settings" 
    android:alpha="0.2"/>  
Run Code Online (Sandbox Code Playgroud)

和代码:

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

所以在开始时我ImageView使用alpha 0.2并且最后我想要使用带有alpha 1的ImageView.但它不能像那样工作 - 当动画开始时添加更多alpha并且动画完成alpha 0.2

我需要更改为0.2到1的动画我的图像?

我检查了不同的设置-我设置0.2,ImageView,1它就像我的预期-从阿尔法1至0.2.它看起来像0.2alpha从动画中乘以alpha ...

Vai*_*wal 85

试试这个

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
animation1.setFillAfter(true);
iv.startAnimation(animation1);
Run Code Online (Sandbox Code Playgroud)


Jul*_* M. 24

可能有点晚了,但在android文档中找到了一个可爱的解决方案.

//In transition: (alpha from 0 to 0.5)
view.setAlpha(0f);
view.setVisibility(View.VISIBLE);
view.animate()
   .alpha(0.5f)
   .setDuration(400)
   .setListener(null);

//Out transition: (alpha from 0.5 to 0)
view.setAlpha(0.5f)
view.animate()
   .alpha(0f)
   .setDuration(400)
   .setListener(new AnimatorListenerAdapter() {
           @Override
           public void onAnimationEnd(Animator animation) {
           view.setVisibility(View.GONE);
         }
    });
Run Code Online (Sandbox Code Playgroud)


Ivá*_*rez 15

1在开始动画之前设置alpha 为我工作:

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(500);
iv.setAlpha(1f);
iv.startAnimation(animation1);
Run Code Online (Sandbox Code Playgroud)

至少在我的测试中,由于在开始动画之前设置了alpha,因此没有闪烁.它运作正常.


ami*_*phy 14

Kotlin Version

ViewPropertyAnimator像这样简单地使用:

iv.alpha = 0.2f
iv.animate().apply {
    interpolator = LinearInterpolator()
    duration = 500
    alpha(1f)
    startDelay = 1000
    start()
}
Run Code Online (Sandbox Code Playgroud)

  • 耶!很好的答案!科特林万岁! (3认同)