1.在res文件夹下创建一个资源文件夹animator(类型为animator)
<?xml version="1.0" encoding="utf-8"?>
<!-- ordering: 执行顺序-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<!-- propertyName:属性名 值rotation: 以屏幕为轴进行旋转 ;floatType:值的类型 -->
<objectAnimator android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360"
android:valueType="floatType"
android:duration="1000"/>
<!-- interpolator: 加速器(可选)使用系统资源;startOffset:停留多少秒再启动-->
<objectAnimator android:propertyName="translationX"
android:duration="1000"
android:startOffset="1000"
android:valueFrom="0"
android:valueTo="600"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>
2.XML文件
<?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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_jump"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到另一个activity"/>
<Button
android:id="@+id/btn_property"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="property动画"/>
<Button
android:id="@+id/btn_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ValueAnimator"/>
<Button
android:id="@+id/btn_obj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ObjectAnimator"/>
</LinearLayout>
3.activity文件
package net.onest.application;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnJump;
private Button btnProperty;
private Button btnValue;
private Button btnObj;
private Animator XmlAnimator;//定义Xml类型的属性动画
private ValueAnimator valueAnimator;//定义ValueAnimator的属性动画
private ObjectAnimator objectAnimator;
private AnimatorSet animatorSet;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
//加载XML描述的属性动画
loadXmlAnimator();
//初始化ValueAnimator
initValueAnimator();
//初始化objectAnimator
initObjectAnimator();
setListeners();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initObjectAnimator() {
//创建集合 管理动画
animatorSet=new AnimatorSet();
objectAnimator=ObjectAnimator.ofFloat(btnObj,//应用目标对象
"alpha",//属性名
1,1.6f,2,1.7f,1.3f,0.5f);
//设置属性
objectAnimator.setDuration(1000);
//设置颜色值得变化
ObjectAnimator colorAnim=ObjectAnimator.ofArgb(btnObj,
"BackgroundColor",
Color.RED,Color.BLUE,Color.GRAY);
//设置平移属性动画
ObjectAnimator tranAnim=ObjectAnimator.ofFloat(btnObj,
"translationY",
0,300,600,400,0);
ObjectAnimator rotationAnim=ObjectAnimator.ofFloat(btnObj,
"rotation",
0,-45,-130,-360,90,180,360);
//设置播放时间
rotationAnim.setDuration(3000);
//按照不定长参数的顺序进行顺序播放
// animatorSet.playSequentially(rotationAnim,tranAnim,colorAnim,objectAnimator);
//不定长参数进行同时播放
// animatorSet.playTogether(rotationAnim,tranAnim,colorAnim,objectAnimator);
//另外一种 控制播放顺序
animatorSet.play(colorAnim).with(objectAnimator).after(rotationAnim).before(tranAnim);
}
/**
*
*/
private void initValueAnimator() {
//valueAnimator对象
valueAnimator=ValueAnimator.ofFloat(1,1.5f,1.8f,2,1.6f,1.2f,0.5f);
//定义动画属性
//设置持续时间
valueAnimator.setDuration(1000);
//设置重复次数
valueAnimator.setRepeatCount(1);
//设置重复方式
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
//创建加速器对象
TimeInterpolator timeInterpolator=new AccelerateInterpolator();
//设置加速器
valueAnimator.setInterpolator(timeInterpolator);
}
/**
* 加载XML描述的属性动画
*/
private void loadXmlAnimator() {
XmlAnimator= AnimatorInflater.loadAnimator(getApplicationContext()
,R.animator.property_anim);
//设置动画关联对象
XmlAnimator.setTarget(btnProperty);
}
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_jump://页面跳转
Intent intent=new Intent();
intent.setClass(MainActivity.this,JumpActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in,R.anim.out);
break;
case R.id.btn_property://加载XML中设置的动画类型
XmlAnimator.start();
break;
case R.id.btn_value://启动valueAnimator动画
if (valueAnimator.isRunning()){
valueAnimator.cancel();
}else{
valueAnimator.start();
}
break;
case R.id.btn_obj://启动ObjectAnimator动画
animatorSet.setTarget(btnObj);
if (animatorSet.isRunning()){
animatorSet.cancel();
}else{
animatorSet.start();
}
break;
}
}
}
private void setListeners() {
MyListener myListener=new MyListener();
btnJump.setOnClickListener(myListener);
btnProperty.setOnClickListener(myListener);
btnValue.setOnClickListener(myListener);
btnObj.setOnClickListener(myListener);
//给ValueAnimator设置属性改变的监听器
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//获取当前状态值
float value= (float) animation.getAnimatedValue();
//给每种动画设置值
//等比例缩放变化
btnValue.setScaleX(value);
btnValue.setScaleY(value);
//水平位移变化
btnValue.setTranslationX(value*100);
//旋转变化
btnValue.setRotationX(value*100);
btnValue.setRotationY(value*100);
}
});
}
private void findViews() {
btnJump=findViewById(R.id.btn_jump);
btnProperty=findViewById(R.id.btn_property);
btnValue=findViewById(R.id.btn_value);
btnObj=findViewById(R.id.btn_obj);
}
}
视频上传不了啊,有需要可以留言那