自定义插值器简单实现

package com.loaderman.customviewdemo;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView)findViewById(R.id.tv);

        findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ValueAnimator animator = ValueAnimator.ofInt(0,300);

                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int curValue = (Integer)animation.getAnimatedValue();
                        tv.layout(tv.getLeft(),curValue,tv.getRight(),curValue+tv.getHeight());
                    }
                });
                animator.setDuration(1000);
                animator.setInterpolator(new MyInterpolator());//使用自定义插值器
                animator.start();
            }
        });
    }


}
package com.loaderman.customviewdemo;

import android.animation.TimeInterpolator;

public class MyInterpolator implements TimeInterpolator {
    public float getInterpolation(float input) {
        return 1-input;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center"
    tools:context="com.loaderman.customviewdemo.MainActivity">

    <Button
        android:id="@+id/start_anim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始动画"/>
    <TextView
        android:id="@+id/tv"
        android:layout_toRightOf="@id/start_anim"
        android:layout_marginLeft="30dp"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:background="#ffff00"/>
</LinearLayout>

效果:

posted on 2018-12-29 14:02  LoaderMan  阅读(355)  评论(0编辑  收藏  举报

导航