废话少说:
- 先来看下Interpolator的api文档(不用翻墙) 上面的图可以看出,Interpolator是一个接口,最根本的接口是TimeInterpolator,下面已经有实现的子类,可以直接拿来用
- 先加速后减速
- 一直加速
- 先向后在向前
- 结束后弹跳一会
- 常量值的改变
- 使用官方的类: 这里实现以上的5个类,剩下的你们自行体会,图片是一个android logo 先来看效果: MainActivity:
package com.huangzhibo.interpolator;import android.media.Image;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AccelerateDecelerateInterpolator;import android.view.animation.AccelerateInterpolator;import android.view.animation.Animation;import android.view.animation.AnticipateInterpolator;import android.view.animation.BounceInterpolator;import android.view.animation.Interpolator;import android.view.animation.LinearInterpolator;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img= (ImageView) findViewById(R.id.imageView); } public void btn(View view){ switch (view.getId()){ case R.id.btn_01: startAnimator(new AccelerateDecelerateInterpolator()); break; case R.id.btn_02: startAnimator(new AccelerateInterpolator()); break; case R.id.btn_03: startAnimator(new AnticipateInterpolator()); break; case R.id.btn_04: startAnimator(new BounceInterpolator()); break; case R.id.btn_05: startAnimator(new LinearInterpolator()); break; } } private void startAnimator(Interpolator interpolator){ Animation animation=new TranslateAnimation(0,500,0,0); animation.setInterpolator(interpolator); animation.setDuration(3000); img.setAnimation(animation); img.startAnimation(animation); }}
到这里相信你已经会使用官方提供的Interpolator类,那么如何自定义自己的Interpolator类呢?
-
从源码入手:
InterPolator: 什么也没有,好吧,接着看TImeInterPolator-> 终于看到曙光了<getInterPolation> 在这里我想我们要实现的方法就是它了.我们可以看下Interpolator的子类->从 下手public class AccelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory { private final float mFactor; private final double mDoubleFactor; public AccelerateInterpolator() { mFactor = 1.0f; mDoubleFactor = 2.0; } /** * Constructor * * @param factor Degree to which the animation should be eased. Seting * factor to 1.0f produces a y=x^2 parabola. Increasing factor above * 1.0f exaggerates the ease-in effect (i.e., it starts even * slower and ends evens faster) */ public AccelerateInterpolator(float factor) { mFactor = factor; mDoubleFactor = 2 * mFactor; } public AccelerateInterpolator(Context context, AttributeSet attrs) { this(context.getResources(), context.getTheme(), attrs); } /** @hide */ public AccelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) { TypedArray a; if (theme != null) { a = theme.obtainStyledAttributes(attrs, R.styleable.AccelerateInterpolator, 0, 0); } else { a = res.obtainAttributes(attrs, R.styleable.AccelerateInterpolator); } mFactor = a.getFloat(R.styleable.AccelerateInterpolator_factor, 1.0f); mDoubleFactor = 2 * mFactor; setChangingConfiguration(a.getChangingConfigurations()); a.recycle(); } public float getInterpolation(float input) { if (mFactor == 1.0f) { return input * input; } else { return (float)Math.pow(input, mDoubleFactor); } } /** @hide */ @Override public long createNativeInterpolator() { return NativeInterpolatorFactoryHelper.createAccelerateInterpolator(mFactor); }
开始分析:三个构造函数 我们来看下只有一个参数的构造函数里面的赋值式子和
return (float)Math.pow(input, mDoubleFactor);
看到没,Math.pow(a,b)函数->是求a的b次方的,那如何改变它的值,不断变大改变速率呢,这里自定义Interpolator,打印log看getInterPolation是不是多次调用就知道了
package com.huangzhibo.interpolator;import android.util.Log;import android.view.animation.Interpolator;/** * Created by HuangZhiBo on 2017/3/12/012. */public class MyInterpolator implements Interpolator{ private float mFactor; private double mDoubleFactor; public MyInterpolator() { mFactor = 0.5f; mDoubleFactor = 2.0; } @Override public float getInterpolation(float input) { if (mFactor == 1.0f) { return input * input; } else { Log.i("info", "getInterpolation: 我被调用了"); return (float)Math.pow(input, mDoubleFactor); } }}
logi的输出图:
可以的出->getInterPolation()被多次调用,所以Math.pow每调用一次input保存上一次值,一直累加....
速度越来越快.
项目地址:
到这里就差不多了,小伙伴们看懂了吗?