【Android动画九章】-动画插值器和动画监听器

一、插值器

我们都知道对于属性动画可以对某个属性做动画,而插值器(TimeInterpolator)在其中扮演了重要角色。
Interpolator的系统值有下面几个:
public interface
TimeInterpolator
android.animation.TimeInterpolator
Known Indirect Subclasses
AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator, AnticipateOvershootInterpolator, BounceInterpolator, CycleInterpolator, DecelerateInterpolator, Interpolator, LinearInterpolator, OvershootInterpolator
也就是:

  • AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速

  • AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速

  • AnticipateInterpolator 开始的时候向后然后向前甩

  • AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值

  • BounceInterpolator 动画结束的时候弹起

  • CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线

  • DecelerateInterpolator 在动画开始的地方快然后慢

  • LinearInterpolator 以常量速率改变

  • OvershootInterpolator 向前甩一定值后再回到原来位置
    下面通过一个例子形象的看一下这些插值器的效果。
    1.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView  android:id="@+id/imageView" android:layout_width="40dp" android:layout_height="40dp" android:layout_margin="10dp" android:src="@drawable/ball" />

    <Spinner  android:id="@+id/spinner" android:layout_width="300dp" android:layout_height="50dp" android:layout_alignParentRight="true"></Spinner>
</RelativeLayout>

2.MainActivity.java:

package demo.androidwar.com.chazhiqi;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    private ArrayList<String> arrayList;
    private ArrayAdapter adapter;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);
        imageView = (ImageView) findViewById(R.id.imageView);
        initData();
        spinner.setPrompt("选择插值器:");
        adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, arrayList);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        ObjectAnimator objectAnimator0 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 1000F);
                        objectAnimator0.setDuration(2000);
                        objectAnimator0.setInterpolator(new AccelerateDecelerateInterpolator());
                        objectAnimator0.start();
                        break;
                    case 1:
                        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 1000F);
                        objectAnimator1.setDuration(2000);
                        objectAnimator1.setInterpolator(new AccelerateInterpolator());
                        objectAnimator1.start();
                        break;
                    case 2:
                        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 1000F);
                        objectAnimator2.setDuration(2000);
                        objectAnimator2.setInterpolator(new CycleInterpolator(7f));
                        objectAnimator2.start();
                        break;
                    case 3:
                        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 1000F);
                        objectAnimator3.setDuration(2000);
                        objectAnimator3.setInterpolator(new LinearInterpolator());
                        objectAnimator3.start();
                        break;
                    case 4:
                        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 1000F);
                        objectAnimator4.setDuration(2000);
                        objectAnimator4.setInterpolator(new BounceInterpolator());
                        objectAnimator4.start();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initData() {
        arrayList = new ArrayList<String>();
        arrayList.add("AccelerateDecelerateInterpolator");
        arrayList.add("AccelerateInterpolator");
        arrayList.add("CycleInterpolator");
        arrayList.add("LinearInterpolator");
        arrayList.add("BounceInterpolator");
    }
}

在动画中引入插值器的直接调用setInterpolator方法,传入一个插值器对象即可,十分方便。下面运行实例,观察一下不同插值器的效果:

二、监听器

监听器可以用来监听动画的状态,通过一个实例进行学习:
1.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <ImageView  android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />

    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onclick" android:text="监听器" />
</LinearLayout>

2.MainActivity.java:

package demo.androidwar.com.jiantingqi;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    public void onclick(View view) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotate", 0F, 360F).setDuration(3000);
        objectAnimator.start();
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Toast.makeText(MainActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

}

调用ObjectAnimator 类的addListener方法,传入AnimatorListener接口对象,需要覆写其四个方法,分别是onAnimationStart(动画开始方法)、onAnimationEnd(动画结束方法)、onAnimationCancel(动画取消方法)、onAnimationRepeat(动画重复方法)。
运行项目如下:
【Android动画九章】-动画插值器和动画监听器_第1张图片
这里在动画结束方法里添加了Toast,动画结束后Toast运行。我们一般不会关注所有方法,但实现AnimatorListener结构必须覆写所有方法,Android还提供了另外一个抽象类供我们继承AnimatorListenerAdapter,下面我们看一下它的用法:

 public void onclick(View view) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotate", 0F, 360F).setDuration(3000);
        objectAnimator.start();
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Toast.makeText(MainActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
            }
        });
    }

选择这个抽象类之后会弹出如下窗口:

选择需要实现的方法即可,这里选择了动画结束的监听,运行项目可以实现和上面一致的结果。
**喜欢的朋友请关注我,另欢迎阅读我的电子书
百度阅读:
http://yuedu.baidu.com/ebook/284b41a1e518964bce847c90?pn=1&click_type=10010002&rf=http%3A%2F%2Fblog.csdn.net%2Fyayun0516%2Farticle%2Fdetails%2F51277821
亚马逊:
http://www.amazon.cn/Android-%E7%99%BE%E6%88%98%E7%BB%8F%E5%85%B8-%E5%8D%B7I-%E5%BC%A0%E4%BA%9A%E8%BF%90/dp/B01ER5R9U2?ie=UTF8&keywords=Android%E7%BB%8F%E5%85%B8&qid=1461806976&ref_=sr_1_6&s=digital-text&sr=1-6**

你可能感兴趣的:(android,属性动画,插值器)