1什么是属性动画:
可以定义任何属性的变化 .通过不断地修改值来实现的,而初始值和结束值间的过度动画就是由该类来负责计算的
API | 说明 |
---|---|
Animator | 创建属性动画的基类,一般不会直接用,一般用他的两个子类! |
ValueAnimator | 上面也说了,属性动画是通过不断地修改值来实现的,而初始值和结束值间的过度动画就是由该类来负责计算的。内部采用一种时间循环的机制来计算值与值之间的动画过度,我们只需将初始值以及结束值提供给该类,并告诉它动画所需时长,该类就会自动帮我们完成从初始值平滑过渡到结束值这样的效果!除此之外,该类还负责管理动画播放次数,播放模式,以及对动画设置监听器等! |
ObjectAnimator | ValueAnimator的子类,允许我们对指定对象的属性执行动画,用起来更加简单,实际中用得较多。当然某些场合下,可能还是需要用到ValueAnimator |
AnimatorSet | Animator的子类,用于组合多个Animator ,并制定多个Animator按照次序播放,还是同时播放 |
Evaluator | 告诉动画系统如何从初始值过度到结束值,提供了下述几种Evaluator : - IntEvaluator :用于计算int类型属性值的计算器 - FloatEvaluator :用于计算float类型属性值的计算器 - ArgbEvaluator :用于计算十六进制形式表示的颜色值的计算器 - TypeEvaluator :计算器的接口,我们可以实现该接口来完成自定义计算器 |
2:使用的流程
别忘了给页面设置一个ID
-
2.1调用ValueAnimator的ofInt(),ofFloat()或ofObject()静态方法创建ValueAnimator实例
-
2.2调用实例的setXxx方法设置动画持续时间,插值方式,重复次数等
-
2.3调用实例的addUpdateListener添加AnimatorUpdateListener监听器,在该监听器中 可以获得ValueAnimator计算出来的值,你可以值应用到指定对象上
-
2.4调用实例的start()方法开启动画! 另外我们可以看到ofInt和ofFloat都有个这样的参数:float/int... values代表可以多个值!
简单的使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ly_root"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="@+id/imageView"
android:layout_width="350dp"
android:layout_height="350dp"
app:srcCompat="@mipmap/a1" />
</LinearLayout>
private ImageView imageView;
private Button button,button2,button3,button4;
//获取页面的ID
private LinearLayout ly_root;
//设置长宽高
private int width;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
button4=findViewById(R.id.button4);
ly_root=findViewById(R.id.ly_root);
imageView=findViewById(R.id.imageView);
//按照轨迹方程来运动的代码
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取页面(总布局)的长宽高
width=ly_root.getWidth();
height=ly_root.getHeight();
//设置初始值
ValueAnimator animator=ValueAnimator.ofInt(height/4,height/2,height/4*3,height);
//动画的持续时间毫秒
animator.setDuration(3000l);
//当动画发生变化时
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//动态y的值
int y=(int)animator.getAnimatedValue();
int x=width/2;
moveView(imageView,x,y);
}
});
//设置动画
animator.setInterpolator(new LinearInterpolator());
//开启动画
animator.start();
}
});
//缩放效果,写了两个方法,都可以实现缩放效果
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// final float scale=0.5f;
// AnimatorSet scaleSet=new AnimatorSet();
// ValueAnimator valueAnimator=ValueAnimator.ofFloat(1.0f,scale);
// ValueAnimator valueAnimator1=ValueAnimator.ofFloat(1.0f,scale);
// valueAnimator.setDuration(500);
// valueAnimator1.setDuration(500);
// valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// float scale=(float) animation.getAnimatedValue();
// imageView.setScaleX(scale);
// imageView.setScaleY(scale);
// }
// });
// valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// float scale=(float)animation.getAnimatedValue();
// imageView.setScaleX(scale);
// imageView.setScaleY(scale);
// }
// });
// scaleSet.play(valueAnimator).after(valueAnimator1);
// scaleSet.start();
ValueAnimator valueAnimator=ValueAnimator.ofFloat(1.0f,0.6f,1.0f,1.0f,0.6f,1.0f);
valueAnimator.setDuration(1000L);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float scale= (float) animation.getAnimatedValue();
imageView.setScaleY(scale);
imageView.setScaleY(scale);
}
});
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.start();
}
});
//旋转的同时透明度变化
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ValueAnimator valueAnimator=ValueAnimator.ofInt(0,720);
valueAnimator.setDuration(2000l);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int rotateValue = (int) animation.getAnimatedValue();
imageView.setRotation(rotateValue);
float fractionValue=animation.getAnimatedFraction();
imageView.setAlpha(fractionValue);
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.start();
}
});
//圆形旋转
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
width=ly_root.getWidth();
height=ly_root.getHeight();
final int R=width/4;
ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,(float)(2.0f*Math.PI));
valueAnimator.setDuration(3000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//圆的参数方程X=R*sin(t)y=R*cos(t)
float t= (float) animation.getAnimatedValue();
int x= (int) (R*Math.sin(t)+width/2);
int y= (int) (R*Math.cos(t)+height/2);
moveView(imageView,x,y);
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.start();
}
});
}
public void moveView(View view,int x,int y){
//
int left=x-view.getHeight()/2;
int top=y-view.getWidth();
int width=left+view.getHeight();
int height=top+view.getHeight();
//设置视图显示在什么位置(left,top显示的定位点|width和height显示图片的大小
view.layout(left,top,width,height);
}