欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

组合动画

程序员文章站 2022-01-28 21:52:59
...
	package com.dingtao.animotordemo;
	
	import android.animation.Animator;
	import android.animation.AnimatorSet;
	import android.animation.ObjectAnimator;
	import android.animation.ValueAnimator;
	import android.support.v7.app.AppCompatActivity;
	import android.os.Bundle;
	import android.view.View;
	import android.view.animation.AccelerateInterpolator;
	import android.view.animation.LinearInterpolator;
	import android.widget.Button;
	import android.widget.ImageView;
	
	public class MainActivity extends AppCompatActivity {
	
	    Button button;
	    ImageView goodsImage;
	
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        button = findViewById(R.id.button);
	//      按钮点击事件
	        button.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	//              设置0到10的整数变化
	                final ValueAnimator valueAnimator = ValueAnimator.ofInt(60,0);
	//              整个事件段是5秒
	                valueAnimator.setDuration(60*1000);
	//              数字均匀变化,也可设置其他的变化方式,先快后慢,先慢后快等......
	                valueAnimator.setInterpolator(new LinearInterpolator());
	//              监听每次改变时的值
	                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
	                    @Override
	                    public void onAnimationUpdate(ValueAnimator animation) {
	                        int value = (Integer) animation.getAnimatedValue();
	                        button.setText(value+"");
	                    }
	                });
	                valueAnimator.start();
	            }
	        });
	
	        goodsImage = findViewById(R.id.goods_image);
	        goodsImage.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                ObjectAnimator translationX = ObjectAnimator.ofFloat(goodsImage,"translationX",0,300f);
	                ObjectAnimator translationY = ObjectAnimator.ofFloat(goodsImage,"translationY",0,500f);
	                ObjectAnimator rotation = ObjectAnimator.ofFloat(goodsImage,"rotation",0,1060);
	
	                AnimatorSet animatorSet = new AnimatorSet();  //组合动画
	                animatorSet.play(translationX).with(rotation).before(translationY);
	//                animatorSet.playTogether(translationX,translationY,rotation); //设置动画
	                animatorSet.setDuration(3000);  //设置动画时间
	                animatorSet.start(); //启动
	            }
	        });
	
	    }
	
	}

动画开始

package com.dingtao.animotordemo;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class MusicButton extends AppCompatImageView implements View.OnClickListener {
    private ObjectAnimator objectAnimator;

    public static final int STATE_PLAYING =1;//正在播放
    public static final int STATE_PAUSE =2;//暂停
    public int state;

    public MusicButton(Context context) {
        super(context);
        init();
    }

    public MusicButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MusicButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        //第一个参数:Object对象

        //第二个参数:属性:旋转:rotation;平移:translationX/translationY
        //缩放:scaleX/scaleY;透明:alpha

        //第三个参数:起始值
        //第四个参数:结束值
        objectAnimator = ObjectAnimator.ofFloat(this,"rotation",0,360);
        objectAnimator.setDuration(1000);
        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        objectAnimator.setInterpolator(new LinearInterpolator());

        objectAnimator.start();//动画开始
        state = STATE_PLAYING;
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (state == STATE_PAUSE){
            objectAnimator.resume();//动画重新开始
            state = STATE_PLAYING;
        }else{
            objectAnimator.pause();//动画暂停
            state = STATE_PAUSE;
        }
    }
}

布局

<com.dingtao.animotordemo.MusicButton
        android:id="@+id/musicButton"
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:srcCompat="@drawable/music" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/goods_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:srcCompat="@mipmap/ic_launcher_round" />
相关标签: 动画