Android Material L 图像过渡插值器

13

这更像是一道数学问题,而不是编程问题。

好的,我想问一下您是否知道 Material Design 中描述的插值器是什么:

enter image description here

它看起来像一个 AccelerateDecelerateInterpolator,但减速效果衰减得更慢。

我最好的猜测是:

public class MaterialInterpolator implements Interpolator {

    @Override
    public float getInterpolation(float input) {
        if(input<1./3f)
            return new AccelerateInterpolator().getInterpolation(input);
        else
            return new DecelerateInterpolator().getInterpolation(input);
    }

}

这会在数值之间产生差距:

Time / Value
...
0.3,0.09
0.317,0.100489
0.333,0.110889  <-- gap
0.35,0.57750005
0.367,0.599311
0.383,0.61931103
0.4,0.64
...
减缓加速减速插值器:
output = accelerateDecelerateInterpolator(decelerateInterpolator(input));

private float accelerateDecelerateInterpolator(float input) {
    return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}

private float decelerateInterpolator(float input) {
    //  return 1.0f - (1.0f - input) * (1.0f - input);
    return  (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));  // default factor =1.f
}

提供类似的费率:

enter image description here

和价值/时间曲线:

enter image description here

不确定开头是输出错误还是实际行为应该是一个输出错误


来源:http://www.google.com/design/spec/patterns/imagery-treatment.html


在最后 (time == 1) 值为0吗? - pskink
@pskink 不是 1.0,而是 1.0fnew DecelerateInterpolator().getInterpolation(1f) 返回 1f)。我猜上面的图表显示的是动画每帧时间(第 x 帧时间减去第 x-1 帧时间)的速率,而不是例如饱和度每帧时间的值。 - Diolor
1
好的,用伪代码表示应该是这样的: AccelerateDecelerateInterpolator.getInterpolation(DecelerateInterpolator.getInterpolation(input)) - pskink
@pskink,所以“减速”A/D插值器?我试过了,可能不是答案,但非常接近。无论如何,Google的示例中没有数字可以确定。添加了一个带有速率的截图。 - Diolor
是的,在扩展AccelerateDecelerateInterpolator时,在getInterpolation中调用input = di.getInterpolation(input),然后调用super,其中di是一个DecelerateInterpolator对象。 - pskink
4个回答

5

这些图看起来非常像x*(a*x+b)^2的图形。 限制第二个根等于1,就像是a*x*(x-1)^2。 对其进行积分,选择a使结果在x=1处为1,并得到3*x^4-8*x^3+6*x^2

代码如下:

public class MaterialInterpolator implements Interpolator {

    @Override
    public float getInterpolation(float x) {
        return (float) (6 * Math.pow(x, 2) - 8 * Math.pow(x, 3) + 3 * Math.pow(x, 4));
    }
}

4

1
Android 5现在为材料设计规范中的三个基本曲线提供插值器:
 * @android:interpolator/fast_out_linear_in
 * @android:interpolator/fast_out_slow_in
 * @android:interpolator/linear_out_slow_in

你最可能需要的是 fast_out_slow_in 插值器。

-1

如果您想在 Android 5.0 之前使用 fast_out_slow_in.xml 插值器,可以在 这里 找到源代码。

<?xml version="1.0" encoding="utf-8"?> <pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:controlX1="0.4" android:controlY1="0" android:controlX2="0.2" android:controlY2="1"/>


2
路径仅存在于5.0或更高版本。 - Anubian Noob

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