参考网址
Android 属性动画:这是一篇全面 & 详细的 属性动画 总结&攻略
布局文件
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="ofInt"
android:id="@+id/btn1"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="ofARGB"
android:id="@+id/btn2"/>
LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonSize();
}
});
btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonColor();
}
});
}
}
/**
* 改变btn的宽高
*/
private void changeButtonSize() {
// 拿到btn1的初始宽度
int width = btn1.getLayoutParams().width;
Log.e("onClick", "width = " + width );
ValueAnimator valueAnimator = ValueAnimator.ofInt(width,1000);
valueAnimator.setDuration(5000);
valueAnimator.setRepeatCount(5);
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue();
// Log.e("onAnimationUpdate", "value = " + value);
// 修改按钮btn1的宽度
btn1.getLayoutParams().width = value;
btn1.getLayoutParams().height = value;
btn1.requestLayout();
}
});
valueAnimator.start();
}
/**
* 改变按钮的颜色
*/
private void changeButtonColor() {
ValueAnimator animator = ValueAnimator.ofArgb(Color.WHITE, Color.RED, Color.BLUE, Color.YELLOW);
animator.setDuration(5000);
animator.setRepeatCount(5);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int color = (int) animation.getAnimatedValue();
btn2.setBackgroundColor(color);
}
});
animator.start();
}
private void changeButtonPosition(){
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btn1.getLayoutParams();
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
valueAnimator.setDuration(3000);
valueAnimator.setRepeatCount(3);
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue();
// Log.e("onAnimationUpdate", "value = " + value);
// 使用这个值来修改view的属性
layoutParams.leftMargin = value;
layoutParams.topMargin = value;
btn1.setLayoutParams(layoutParams);
btn1.requestLayout();
}
});
valueAnimator.start();
}
/**
* 组合使用属性动画
*/
private void useAnimatorSet() {
AnimatorSet animatorSet = new AnimatorSet();
ValueAnimator animator = ValueAnimator.ofInt(0, 1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btn4.getLayoutParams();
params.topMargin = value;
btn4.requestLayout();
}
});
ValueAnimator animator2 = ValueAnimator.ofArgb(Color.RED, Color.BLUE);
animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int color = (int) animation.getAnimatedValue();
btn4.setBackgroundColor(color);
}
});
animatorSet.setDuration(5000);
// animatorSet.playTogether(animator, animator2);
animatorSet.play(animator).with(animator2);
animatorSet.start();
}
ValueAnimator animator = ValueAnimator.ofFloat(0.0F, 1000F);
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
Log.e("onAnimationUpdate","value = " + value);
}
});
animator.start();
第一步 创建MyView 继承系统的View 注意两个参数的构造函数一定要实现 因为布局文件绘制时要用到
package com.example.demo;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
public class MyView extends View {
Paint paint = new Paint();
Point curPoint = new Point(200, 200);
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
canvas.drawCircle(curPoint.x,curPoint.y,100, paint);
}
}
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画"
android:id="@+id/btn_start"/>
<com.example.demo.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myview"/>
LinearLayout>
此时可以先看一下有没有绘制出一个小的圆形
package com.example.demo;
import android.animation.TypeEvaluator;
import android.graphics.Point;
public class PointEvaluator implements TypeEvaluator<Point> {
@Override
public Point evaluate(float fraction, Point startValue, Point endValue) {
int deltaX = endValue.x - startValue.x;
int deltaY = endValue.y - startValue.y;
int curX = (int) (startValue.x + fraction * deltaX);
int curY = (int) (startValue.y + fraction * deltaY);
Point point = new Point(curX, curY);
return point;
}
}
public void startAnim() {
Point startPoint = new Point(200, 200);
Point endPoint = new Point(600, 800);
ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(),
startPoint, endPoint);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
curPoint = (Point) animation.getAnimatedValue();
Log.e("onAnimationUpdate", "curPoint = " + curPoint);
invalidate();
}
});
valueAnimator.start();
}
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnStart;
private MyView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = findViewById(R.id.btn_start);
myView = findViewById(R.id.myview);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myView.startAnim();
}
});
}
}
float translationX = btn_alpha.getTranslationX();
ObjectAnimator animator = ObjectAnimator.ofFloat(btn_alpha,
"translationX", translationX, 800, translationX);
animator.setDuration(5000);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(btn_alpha, "scaleX", 1f, 5f, 1f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(btn_alpha, "scaleY", 1f, 5f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(animator).with(animator2);
animatorSet.setDuration(5000);
animatorSet.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(btn_alpha,
"rotation", 0, 360);
animator.setDuration(5000);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(btn_alpha,
"alpha", 1, 0, 1);
animator.setDuration(5000);
animator.start();