25属性动画
- 属性动画是建立在补间动画之上
当A图从X移动Y时 A图的属性也会跟着变化 并且有对应的监听方法 也就是说当A图从X到Y时 你可以点击Y地区触发A图的点击方法
代码创建属性动画:
- 创建实例类 : ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(控件id,动画类型,可变的浮点类型);
- 设置播放动画时间 : rotateAnimator.setDuration(5000);
- 开始播放 : rotateAnimator.start();
- xml属性动画:
- 在res创建文件夹animator创建相应的动画
Animator animator = AnimatorInflater.loadAnimator(上下文,动画文件id);
animator.setTarget(iv);//设置操作对象
animator.start();
案例结构
xml_object.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
>
<objectAnimator
android:propertyName="scaleY"
android:valueFrom="1"
android:valueTo="3"
android:duration="3000"
></objectAnimator>
<set
android:ordering="together"
>
<objectAnimator
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="200"
android:duration="3000"
></objectAnimator>
<objectAnimator
android:propertyName="translationY"
android:valueFrom="0"
android:valueTo="500"
android:duration="3000"
></objectAnimator>
</set>
</set>
background_object.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="backgroundColor"
android:duration="5000"
android:valueFrom="#ff0000"
android:valueTo="#0000ff"
android:valueType="intType"
>
</objectAnimator>
MainActivity.java
package com.qf.sxy.propertyanimation;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = ((ImageView) findViewById(R.id.iv));
}
public void MyClick(View view) {
switch (view.getId()){
case R.id.btn_alpha:
/**
* 获取属性动画
* 参数1:执行动画的对象
* 参数2:动画的属性名
* 参数3:可变参数 执行动画的值
*/
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(iv,"alpha",1.0f,0.0f);
alphaAnimator.setDuration(5000);
//对动画进行监听
alphaAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
//动画开始的时候
Log.e("AAA","=Start=>");
}
@Override
public void onAnimationEnd(Animator animation) {
//动画结束的时候
Log.e("AAA","=END=>");
}
@Override
public void onAnimationCancel(Animator animation) {
//动画取消的时候
Log.e("AAA","=Cancel=>");
// alphaAnimator.cancel();调用此方法取消动画调用
}
@Override
public void onAnimationRepeat(Animator animation) {
//动画重复时候
Log.e("AAA","=Repeat=>");
}
});
alphaAnimator.start();//启动动画
break;
case R.id.btn_rotate:
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(iv,"rotation",0,-360,720);
rotateAnimator.setDuration(5000);
rotateAnimator.start();
break;
case R.id.btn_scale:
ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(iv,"scaleY",1,4,2,0.5f);
scaleAnimator.setDuration(5000);
scaleAnimator.start();
break;
case R.id.btn_translate:
ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(iv,"translationY",0,200,50,300);
translateAnimator.setDuration(5000);
translateAnimator.start();
break;
case R.id.btn_set:
//获取属性动画的集合对象
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator translateY = ObjectAnimator.ofFloat(iv,"translationY",0,200,100,300);
translateY.setDuration(3000);
ObjectAnimator rotateY = ObjectAnimator.ofFloat(iv,"rotation",0,-720,720);
rotateY.setDuration(3000);
//第一种方式 执行 note:同一个动画只能执行一次
// play(),
// with(),translateY和with内的动画一起执行
// before(),translateY相对于before内的动画前执行
// after() translateY相对于after内的动画后执行
// animatorSet.play(translateY).with(rotateY);
// animatorSet.start();
//第二种方式
List<Animator> list = new ArrayList<>();
list.add(translateY);
list.add(rotateY);
// animatorSet.playTogether(list);//同步执行
animatorSet.playSequentially(list);//依次执行
//第三种可变参数
// animatorSet.playSequentially(rotateY,translateY);
animatorSet.start();
break;
case R.id.btn_xml:
//使用动画填充器 将xml布局的动画添加进来
Animator animator = AnimatorInflater.loadAnimator(MainActivity.this,R.animator.background_object);
animator.setTarget(iv);//设置操作对象
animator.start();
// //使用动画填充器 将xml布局的动画添加进来
// Animator animator = AnimatorInflater.loadAnimator(MainActivity.this,R.animator.xml_object);
// animator.setTarget(iv);//设置操作对象
// animator.start();
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qf.sxy.tweenanimation.MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="MyClick"
android:text="透明"/>
<Button
android:id="@+id/btn_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="MyClick"
android:text="旋转"/>
<Button
android:id="@+id/btn_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="MyClick"
android:text="缩放"/>
<Button
android:id="@+id/btn_translate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="MyClick"
android:text="位移"/>
<Button
android:id="@+id/btn_set"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="MyClick"
android:text="集合"/>
<Button
android:id="@+id/btn_xml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="MyClick"
android:text="xml"/>
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:src="@mipmap/rw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll" />
</RelativeLayout>