android 动画的Interpolator插值器

  •    an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the
    
  •    ease-out effect (i.e., it starts even faster and ends evens slower)
    

*/

public DecelerateInterpolator(float factor) {

mFactor = factor;

}

public DecelerateInterpolator(Context context, AttributeSet attrs) {

this(context.getResources(), context.getTheme(), attrs);

}

/** @hide */

public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {

TypedArray a;

if (theme != null) {

a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0);

} else {

a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);

}

mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);

a.recycle();

}

public float getInterpolation(float input) {

float result;

if (mFactor == 1.0f) {

result = (float)(1.0f - (1.0f - input) * (1.0f - input));

} else {

result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));

}

return result;

}

private float mFactor = 1.0f;

/** @hide */

@Override

public long createNativeInterpolator() {

return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);

}

}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

===

C++ 的版本实现 :

===========

float Elastic::easeIn (float t, float b , float c, float d) {

if (t==0) return b; if ((t/=d)==1) return b+c;

float p=d*.3f;

float a=c;

float s=p/4;

float postFix =apow(2,10(t-=1)); // this is a fix, again, with post-increment operators

return -(postFix * sin((td-s)(2*PI)/p )) + b;

}

float Elastic::easeOut(float t,float b , float c, float d) {

if (t==0) return b; if ((t/=d)==1) return b+c;

float p=d*.3f;

float a=c;

float s=p/4;

return (apow(2,-10t) * sin( (td-s)(2*PI)/p ) + c + b);

}

float Elastic::easeInOut(float t,float b , float c, float d) {

if (t==0) return b; if ((t/=d/2)==2) return b+c;

float p=d*(.3f*1.5f);

float a=c;

float s=p/4;

if (t < 1) {

float postFix =apow(2,10(t-=1)); // postIncrement is evil

return -.5f*(postFix* sin( (td-s)(2*PI)/p )) + b;

}

float postFix = apow(2,-10(t-=1)); // postIncrement is evil

return postFix * sin( (td-s)(2PI)/p ).5f + c + b;

}

参数的意思:

  • t – 动画中当前的时间

  • b – 开始值

  • c – 结束值

  • d – 动画的总时间

看下Java的第一行前三个的:

public class Sine {

public static float easeIn(float t,float b , float c, float d) {

return -c * (float)Math.cos(t/d * (Math.PI/2)) + c + b;

}

public static float easeOut(float t,float b , float c, float d) {

return c * (float)Math.sin(t/d * (Math.PI/2)) + b;

}

public static float easeInOut(float t,float b , float c, float d) {

return -c/2 * ((float)Math.cos(Math.PI*t/d) - 1) + b;

}

}

虽然 Java 的也有了,但是话说这个怎么用啊,跟上面的Interpolator如何联系起来啊?

一个简单的方法:首先把 d 总时间设置为固定值 1.0 ,把 b 开始值设置为 0.0 把结束值设置为1.0,然后把 t 当作上面 Interpolator 中的 float getInterpolation(float input);传入值,此时不就能用上了。

举个Case

======

/**

  • Created by Qiujuer on 2015/1/5.

*/

public class InSineInterpolator implements Interpolator{

public static float easeIn(float t,float b , float c, float d) {

return -c * (float)Math.cos(t/d * (Math.PI/2)) + c + b;

}

@Override

public float getInterpolation(float input) {

return easeIn(input, 0, 1, 1);

}

}

使用

==

//AnimatorSet

mAnimatorSet = new AnimatorSet();

mAnimatorSet.playTogether(aPaintX, aPaintY, aRadius, aBackground);

mAnimatorSet.setInterpolator(new InSineInterpolator());

mAnimatorSet.start();

可以看出使用与上面 Android 自带的完全一样,当然这个只是个 Case ,具体使用中你可以随意封装,前提是别改动了主要部分。

/**

  • An interpolator where the rate of change starts out quickly and

  • and then decelerates.

*/

@HasNativeInterpolator

public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {

public DecelerateInterpolator() {

}

/**

  • Constructor

  • @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces

  •    an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the
    
  •    ease-out effect (i.e., it starts even faster and ends evens slower)
    

*/

public DecelerateInterpolator(float factor) {

mFactor = factor;

}

public DecelerateInterpolator(Context context, AttributeSet attrs) {

this(context.getResources(), context.getTheme(), attrs);

}

/** @hide */

public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {

TypedArray a;

if (theme != null) {

a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0);

} else {

a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);

}

mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);

a.recycle();

}

public float getInterpolation(float input) {

float result;

if (mFactor == 1.0f) {

result = (float)(1.0f - (1.0f - input) * (1.0f - input));

} else {

result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));

}

return result;

}

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

移动架构师

系统学习技术大纲

一线互联网Android面试题总结含详解(初级到高级专题)

image

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

[外链图片转存中…(img-jmycI338-1714677692723)]

[外链图片转存中…(img-nBiRAgjK-1714677692723)]

一线互联网Android面试题总结含详解(初级到高级专题)

[外链图片转存中…(img-cpekCqko-1714677692723)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 29
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值