猿问

淡入淡出动画播放两次

每当用户在上键入内容时,我都将启用按钮的可见性(从GONE到VISIBLE),并设置其alpha值的动画EditText。


private void ShowSendButton()

    {

        if (sendbtn.Visibility != ViewStates.Visible)

        {

            sendbtn.Visibility = ViewStates.Visible;

            sendbtn.Animate().Alpha(1.0f);

        }


    }

无论何时EditText为空,按钮都会淡出并且其可见性设置为GONE,如下所示:


private void HideSendButton()

        {

            if (sendbtn.Visibility != ViewStates.Gone)

            {

                sendbtn.Animate().Alpha(0.0f).SetDuration(150).Start();

                sendbtn.Visibility = ViewStates.Gone;

            }

        }

该ShowSendButton动画始终发挥两次,动画结束后,即右,按钮的alpha值重置回0.0,再次播放动画渐变。


在HideSendButton动画中不会发生这种情况。


这是触发动画的代码:


editText.TextChanged += (s, e) =>

            {

                if (e.AfterCount != 0)

                {

                    ShowSendButton();

                }

                else

                {

                    HideSendButton();

                }


            };

这是按钮XML:


<Button

            android:id="@+id/sendbtn"

            android:layout_width="40dp"

            android:layout_height="40dp

            android:textColor="#fff"

            android:visibility="gone"

            android:alpha="0"/>


潇潇雨雨
浏览 175回答 2
2回答

青春有我

试试这些:private void ShowSendButton(){&nbsp; &nbsp; if (sendbtn.Visibility != ViewStates.Visible)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; sendbtn.ClearAnimation();&nbsp; &nbsp; &nbsp; &nbsp; sendbtn.Animate().Alpha(1.0f).SetDuration(200).WithStartAction(new Runnable(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendbtn.Visibility = ViewStates.Visible;&nbsp; &nbsp; &nbsp; &nbsp; })).Start();&nbsp; &nbsp; }}private void HideSendButton(){&nbsp; &nbsp; if (sendbtn.Visibility != ViewStates.Gone)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; sendbtn.ClearAnimation();&nbsp; &nbsp; &nbsp; &nbsp; sendbtn.Animate().Alpha(0.0f).SetDuration(200).WithEndAction(new Runnable(() =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendbtn.Visibility = ViewStates.Gone;&nbsp; &nbsp; &nbsp; &nbsp; })).Start();&nbsp; &nbsp; }}在设备上进行了测试,效果似乎不错。希望能帮助到你。-

智慧大石

似乎通过将我的ShowSendButton代码更改为部分固定private void ShowSendButton()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (sendbtn.Visibility != ViewStates.Visible)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendbtn.Visibility = ViewStates.Visible;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendbtn.ClearAnimation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Animation fadeIn = new AlphaAnimation(0, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fadeIn.Duration = 50;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendbtn.Animation = fadeIn;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }工作正常,但我似乎无法更改动画的持续时间(fadeIn.Duration = 50不起作用)。无论我将其设置为什么值,它始终会以约0.2s的速度播放。
随时随地看视频慕课网APP
我要回答