一.简介
1.定义
Android实现动画效果中的一个辅助接口。
2.作用
设置 属性值 从初始值过渡到结束值 的变化规律,实现非线性运动的动画效果。
二.使用
1.Xml方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android"
// 通过资源ID设置插值器 android:interpolator="@android:anim/overshoot_interpolator" android:fromXScale="0.0" android:fromYScale="0.0" android:toXScale="2" android:toYScale="2" android:pivotX="50%" android:pivotY="50%" android:duration="3000" />
|
2.Java代码
1
| animation.setInterpolator(overshootInterpolator);
|
三.系统内置插值器
1.类型
系统默认的插值器是AccelerateDecelerateInterpolator
,即先加速后减速。

2.效果

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
final TranslateAnimation translateAnimation = new TranslateAnimation(0, 500, 0, 0); translateAnimation.setDuration(2000);
findViewById(R.id.accelerate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new AccelerateInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.overshoot).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new OvershootInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.ac_de_celerate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.anticipate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new AnticipateInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.anti_overshoot).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new AnticipateOvershootInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.bounce).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new BounceInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.cycle).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new CycleInterpolator(1.5f));
v.startAnimation(translateAnimation); } });
findViewById(R.id.decelerate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new DecelerateInterpolator());
v.startAnimation(translateAnimation); } });
findViewById(R.id.linear).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { translateAnimation.setInterpolator(new LinearInterpolator());
v.startAnimation(translateAnimation); } }); } }
|
四.自定义插值器
1.本质
根据动画的进度(0%-100%)计算出当前属性值改变的百分比
2.实现方式
自定义插值器需要实现 Interpolator
/ TimeInterpolator
接口 & 重写getInterpolation()
- 补间动画 实现
Interpolator
接口;属性动画实现TimeInterpolator
接口
TimeInterpolator
接口是属性动画中新增的,用于兼容Interpolator
接口,这使得所有过去的Interpolator
实现类都可以直接在属性动画使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public interface Interpolator {
float getInterpolation(float input) { ...
return xxx; }
public interface TimeInterpolator { float getInterpolation(float input); }
|
3.系统的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @HasNativeInterpolator public class LinearInterpolator extends BaseInterpolator implements NativeInterpolatorFactory { ... public float getInterpolation(float input) { return input; }
@HasNativeInterpolator public class AccelerateDecelerateInterpolator implements Interpolator, NativeInterpolatorFactory { ... public float getInterpolation(float input) { return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f; } }
|
4.自定义 Interpolator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class DecelerateAccelerateInterpolator implements Interpolator {
@Override public float getInterpolation(float input) { float result; if (input <= 0.5) { result = (float) (Math.sin(Math.PI * input)) / 2; } else { result = (float) (2 - Math.sin(Math.PI * input)) / 2; } return result; } }
|