版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I123456789T/article/details/89351675
再次记录一下用到的几个动画实现,使用的是ObjectAnimator 实现
animation布局,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_animation"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:src="@mipmap/thumbnail"/>
<!---->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度变化"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放变化到消失"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转变化"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位移变化"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<Button
android:id="@+id/btn_start4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转后位移"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重复透明,闪烁"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重复位移,抖动"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态改变颜色"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_start8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从消失到显示"
android:layout_weight="1"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
activity代码:
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.ImageView;
import com.example.weiwenyi.androidtest.util.Util;
/**
* Created by weiwenyi on 2018/3/21.
*/
public class AnimationActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView animation;
private Button btn_start,btn_start1,btn_start2,btn_start3,btn_start4,btn_start5,btn_start6,btn_start7,btn_start8;
ObjectAnimator anim,anim1,anim2,anim3;
PropertyValuesHolder objectAniationX,objectAniationY,objectAniationX1,objectAniationY1;
boolean isGone = true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
animation = findViewById(R.id.img_animation);
btn_start = findViewById(R.id.btn_start);
btn_start1 = findViewById(R.id.btn_start1);
btn_start2 = findViewById(R.id.btn_start2);
btn_start3 = findViewById(R.id.btn_start3);
btn_start4 = findViewById(R.id.btn_start4);
btn_start5 = findViewById(R.id.btn_start5);
btn_start6 = findViewById(R.id.btn_start6);
btn_start7 = findViewById(R.id.btn_start7);
btn_start8 = findViewById(R.id.btn_start8);
btn_start.setOnClickListener(this);
btn_start1.setOnClickListener(this);
btn_start2.setOnClickListener(this);
btn_start3.setOnClickListener(this);
btn_start4.setOnClickListener(this);
btn_start5.setOnClickListener(this);
btn_start6.setOnClickListener(this);
btn_start7.setOnClickListener(this);
btn_start8.setOnClickListener(this);
// alphaSet();
scaleSet();
scaleSet1();
rotationSet();
translationSet();
scaleToRotation();
translationToRotation();
alpha();
translateX();
backgroundColor();
}
/**
* 透明度
*/
AnimatorSet animationSetS;
@SuppressLint("WrongConstant")
private void alphaSet(){
animationSetS = new AnimatorSet();
ObjectAnimator anim = ObjectAnimator.ofFloat(animation,"alpha",0f,3f);
// anim.setRepeatCount(1000);
anim.setRepeatMode(ValueAnimator.INFINITE);
ObjectAnimator anim1 = ObjectAnimator.ofFloat(animation,"alpha",3f,0f);
anim1.setRepeatCount(1000);
animationSetS.playTogether(anim1,anim);
animationSetS.setDuration(5000);
// animationSetS.play(anim).before(anim1);
}
// 透明度
@SuppressLint("WrongConstant")
private void alphaAnimation() {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(animation, "alpha", 1.0f, 0F);
objectAnimator.setDuration(4000);
// objectAnimator.setRepeatCount(Animation.INFINITE);
objectAnimator.setRepeatMode(Animation.RESTART);
objectAnimator.start();
}
/**
* 缩放
*/
private void scaleSet(){
objectAniationX = PropertyValuesHolder.ofFloat("scaleX",1f,0f);
objectAniationY = PropertyValuesHolder.ofFloat("scaleY",1f,0f);
}
/**
* 缩放
*/
private void scaleSet1(){
objectAniationX1 = PropertyValuesHolder.ofFloat("scaleX",0f,1f);
objectAniationY1 = PropertyValuesHolder.ofFloat("scaleY",0f,1f);
}
/**
* 旋转
*/
private void rotationSet(){
anim1 = ObjectAnimator.ofFloat(animation,"rotation",0f,360f);
anim1.setDuration(5000);
}
/**
* 位移
*/
AnimatorSet translatSetl, translatSeth;
private void translationSet(){
translatSetl = new AnimatorSet();
translatSeth = new AnimatorSet();
// anim2 = ObjectAnimator.ofFloat(animation,"translationX",0f,300f);
// anim3 = ObjectAnimator.ofFloat(animation,"translationY",0f,-200f);
ObjectAnimator animX = ObjectAnimator.ofFloat(animation,"translationX",0f,300f);
ObjectAnimator animY = ObjectAnimator.ofFloat(animation,"translationY",0f,-300f);
translatSetl.setDuration(4000);
translatSetl.play(animX).with(animY);
ObjectAnimator animXh = ObjectAnimator.ofFloat(animation,"translationX",300f,0f);
ObjectAnimator animYh = ObjectAnimator.ofFloat(animation,"translationY",-300f,0f);
translatSeth.setDuration(4000);
translatSeth.play(animXh).with(animYh);
// anim2.setDuration(5000);
// anim3.setDuration(5000);
}
/**
*先播放动画,然后旋转
*/
AnimatorSet animationSets ,animatorSet1;
private void scaleToRotation(){
animationSets = new AnimatorSet();
animatorSet1 = new AnimatorSet();
ObjectAnimator scaleX = ObjectAnimator.ofFloat(animation,"scaleX",0f,1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(animation,"scaleY",0f,1f);
// ObjectAnimator rotationX = ObjectAnimator.ofFloat(animation,"rotationX",0f,360f);
ObjectAnimator retationY = ObjectAnimator.ofFloat(animation,"rotationY",0f,360f);
animationSets.setDuration(1000);
animationSets.play(scaleX).with(scaleY).with(retationY);
ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(animation,"scaleX",1f,0f);
ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(animation,"scaleY",1f,0f);
ObjectAnimator retationY1 = ObjectAnimator.ofFloat(animation,"rotationY",0f,-360f);
animatorSet1.setDuration(1000);
animatorSet1.play(scaleX1).with(scaleY1).with(retationY1);
}
/**
* 先旋转,在位移
*/
AnimatorSet animationSets1;
private void translationToRotation(){
animationSets1 = new AnimatorSet();
ObjectAnimator translateX = ObjectAnimator.ofFloat(animation,"translationX",0f,500f);
ObjectAnimator rotationX = ObjectAnimator.ofFloat(animation,"rotationX",0f,360f);
ObjectAnimator rotationY = ObjectAnimator.ofFloat(animation,"rotationY",0f,360f);
animationSets1.setDuration(1000);
animationSets1.play(translateX).after(rotationX).with(rotationY);
animationSets1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
Util.showToast("开始");
}
@Override
public void onAnimationEnd(Animator animator) {
Util.showToast("结束");
}
@Override
public void onAnimationCancel(Animator animator) {
Util.showToast("取消");
}
@Override
public void onAnimationRepeat(Animator animator) {
Util.showToast("重复");
}
});
}
/**
*闪烁
*/
ObjectAnimator alpha;
private void alpha(){
alpha = ObjectAnimator.ofFloat(animation,"alpha",0f,1f);
alpha.setDuration(500);
alpha.setRepeatCount(3);
}
/**
* 位移抖动
*/
ObjectAnimator tanslate;
private void translateX(){
tanslate = ObjectAnimator.ofFloat(animation,"translationX",-50f,50f);
tanslate.setDuration(500);
tanslate.setRepeatCount(3);
}
/**
* 动态改变颜色
*/
ObjectAnimator back;
@SuppressLint("ObjectAnimatorBinding")
private void backgroundColor(){
back = ObjectAnimator.ofFloat(animation,"backgroundColor", Color.BLUE,Color.YELLOW,Color.RED);
back.setDuration(5000);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_start:
// anim.start();
alphaAnimation();
break;
case R.id.btn_start1: // 从缩放消失
ObjectAnimator.ofPropertyValuesHolder(animation,objectAniationX,objectAniationY).setDuration(5000).start();
break;
case R.id.btn_start2:
anim1.start();
break;
case R.id.btn_start3:
if (isGone){
isGone = false;
// anim2.start();
translatSetl.start();
}else {
isGone = true;
// anim3.start();
translatSeth.start();
}
break;
case R.id.btn_start4:
animationSets.start();
break;
case R.id.btn_start5:
animationSets1.start();
break;
case R.id.btn_start6:
tanslate.start();
animationSets1.start();
break;
case R.id.btn_start7:
animatorSet1.start();
break;
case R.id.btn_start8: // 消失到显示
ObjectAnimator.ofPropertyValuesHolder(animation,objectAniationX1,objectAniationY1).setDuration(5000).start();
break;
}
}
}
动画的实现,是通过对一张图片进行的处理,选择,透明度,位移,等等
记录一下,以备下次使用