属性动画
新引入的属性动画机制已经不再是针对于View来设计的了,也不限定于只能实现移动、缩放、旋转和淡入淡出这几种动画操作,同时也不再只是一种视觉上的动画效果了。它实际上是一种不断地对值进行操作的机制,并将值赋值到指定对象的指定属性上,可以是任意对象的任意属性。所以我们仍然可以将一个View进行移动或者缩放,但同时也可以对自定义View中的Point对象进行动画操作了。我们只需要告诉系统动画的运行时长,需要执行哪种类型的动画,以及动画的初始值和结束值,剩下的工作就可以全部交给系统去完成了。
属性动画主要分为两个大块,一个是ValueAnimtor,另一个是ObjectAnimator
ValueAnimator
ValueAnimator是整个属性动画机制当中最核心的一个类,前面我们已经提到了,属性动画的运行机制是通过不断地对值进行操作来实现的,而初始值和结束值之间的动画过渡就是由ValueAnimator这个类来负责计算的。它的内部使用一种时间循环的机制来计算值与值之间的动画过渡,我们只需要将初始值和结束值提供给ValueAnimator,并且告诉它动画所需运行的时长,那么ValueAnimator就会自动帮我们完成从初始值平滑地过渡到结束值这样的效果。除此之外,ValueAnimator还负责管理动画的播放次数、播放模式、以及对动画设置监听器等,确实是一个非常重要的类。
ObjectAnimator
相比于ValueAnimator,ObjectAnimator可能才是我们最常接触到的类,因为ValueAnimator只不过是对值进行了一个平滑的动画过渡,但我们实际使用到这种功能的场景好像并不多。而ObjectAnimator则就不同了,它是可以直接对任意对象的任意属性进行动画操作的,比如说View的alpha属性。
不过虽说ObjectAnimator会更加常用一些,但是它其实是继承自ValueAnimator的,底层的动画实现机制也是基于ValueAnimator来完成的,因此ValueAnimator仍然是整个属性动画当中最核心的一个类。那么既然是继承关系,说明ValueAnimator中可以使用的方法在ObjectAnimator中也是可以正常使用的,它们的用法也非常类似
简单的介绍就到这了,接下来写代码,效果图:
简图:
布局文件:activity_main.xml:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/id_container" >
- <LinearLayout
- android:id="@+id/btns"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:id="@+id/jianbian"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="渐变"/>
- <Button
- android:id="@+id/pingyi"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="平移"/>
- <Button
- android:id="@+id/xuanzhuan"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="旋转"/>
- <Button
- android:id="@+id/suofang"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="缩放"/>
- <Button
- android:id="@+id/jihe"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="集合"/>
- </LinearLayout>
- <ImageView
- android:layout_below="@id/btns"
- android:id="@+id/id_ball"
- android:layout_width="400dp"
- android:layout_height="400dp"
- android:layout_centerInParent="true"
- android:src="@mipmap/a"
- android:scaleType="centerCrop"
- android:onClick="rotateyAnimRun"
- />
- </RelativeLayout>
- package com.eightgroup.rikao1014;
- import android.animation.AnimatorSet;
- import android.animation.ObjectAnimator;
- import android.animation.ValueAnimator;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- public class MainActivity extends AppCompatActivity {
- @BindView(R.id.jianbian)
- Button jianbian;
- @BindView(R.id.pingyi)
- Button pingyi;
- @BindView(R.id.xuanzhuan)
- Button xuanzhuan;
- @BindView(R.id.suofang)
- Button suofang;
- @BindView(R.id.jihe)
- Button jihe;
- @BindView(R.id.id_container)
- RelativeLayout idContainer;
- @BindView(R.id.id_ball)
- ImageView idBall;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- }
- @OnClick({R.id.jianbian, R.id.pingyi, R.id.xuanzhuan, R.id.suofang, R.id.jihe})
- public void onViewClicked(final View view) {
- switch (view.getId()) {
- case R.id.jianbian:
- ObjectAnimator animator = ObjectAnimator.ofFloat(idBall, "alpha", 1f, 0f, 1f);
- animator.setDuration(1000);
- animator.start();
- break;
- case R.id.pingyi:
- float curTranslationX = idBall.getTranslationX();
- animator = ObjectAnimator.ofFloat(idBall, "translationX", curTranslationX, -500f, curTranslationX);
- animator.setDuration(5000);
- animator.start();
- break;
- case R.id.xuanzhuan:
- ObjectAnimator//
- .ofFloat(idBall, "rotationX", 0.0F, 360.0F)//
- .setDuration(500)//
- .start();
- break;
- case R.id.suofang:
- ObjectAnimator anim = ObjectAnimator//
- .ofFloat(idBall, "zhy", 1.0F, 0.0F)//
- .setDuration(500);//
- anim.start();
- anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @Override
- public void onAnimationUpdate(ValueAnimator animation) {
- float cVal = (Float) animation.getAnimatedValue();
- idBall.setAlpha(cVal);
- idBall.setScaleX(cVal);
- idBall.setScaleY(cVal);
- }
- });
- break;
- case R.id.jihe:
- ObjectAnimator moveIn = ObjectAnimator.ofFloat(idBall, "translationX", -500f, 0f);
- ObjectAnimator rotate = ObjectAnimator.ofFloat(idBall, "rotation", 0f, 360f);
- ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(idBall, "alpha", 1f, 0f, 1f);
- AnimatorSet animSet = new AnimatorSet();
- animSet.play(rotate).with(fadeInOut).after(moveIn);
- animSet.setDuration(5000);
- animSet.start();
- break;
- }
- }
- }
到这就基本上完成了,