动画没有显示介于

时间:2017-12-01 18:34:58

标签: xamarin xamarin.forms

我已经创建了一个简单的功能,可以动画"动画"在水龙头处的细胞背景颜色,它完美地工作:

        Color nOldColor = _grid.BackgroundColor;

        for (int i = 0; i <= 100; i += 5)
        {
            double f = (double)i / (double)100;
            Color nNewColor = PCLHelper.BlendColors(nOldColor, Color.Red, f);
            _grid.BackgroundColor = nNewColor;
            _label1.BackgroundColor = nNewColor;
            await Task.Delay(5);
        }
        _grid.BackgroundColor = nOldColor;
        _label1.BackgroundColor = nOldColor;

现在我想对动画做同样的事情,但是动画并没有显示步骤&#34;中间&#34;而是(因为它在我看来)切换到最终颜色:

    private async void animateButtonTouched()
    {

        int repeatCountMax = 100;
        Color nOldColor = _grid.BackgroundColor;

        var repeatCount = 0;
        _grid.Animate("changeBG", new Animation((val) =>
        {
            double f = (double)repeatCount / (double)100;
            Color nNewColor = PCLHelper.BlendColors(nOldColor, Color.Red, f);
            _grid.BackgroundColor = nNewColor;
            _label1.BackgroundColor = nNewColor;
        }),
        5, //duration. I've also tried it with 100. Nothing helped
        finished: (val, b) =>
        {
            repeatCount++;
        }, repeat: () =>
        {
            return repeatCount < repeatCountMax;
        });

我做错了什么?

1 个答案:

答案 0 :(得分:1)

“你使它变得比它需要的更难。”待定商标

Animate回调提供了步进值(或关键帧值)。这是从0到1的double,在X毫秒(默认值为250)的过程中称为X毫秒(即单个动画帧的长度,默认值为16)。

因此,在此示例中,ShiftToColor被调用125次(2000/16),其值从0到1均匀分配,因此步长为.008。

var orgColor = aFormsElementInstance.BackgroundColor;
aFormsElementInstance.Animate("changeBG", new Animation((val) =>
{
    Color ShiftToColor(Color From, Color To, double pct)
    {
        var r = From.R + ((To.R - From.R) * val);
        var g = From.G + ((To.G - From.G) * val);
        var b = From.B + ((To.B - From.B) * val);
        return new Color(r, g, b);
    }
    Device.BeginInvokeOnMainThread(() =>
    {
        aFormsElementInstance.BackgroundColor = ShiftToColor(orgColor, Color.Red, val);
    });
}), 16, 2000);

结果:

enter image description here