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

Animation

程序员文章站 2024-01-19 13:47:46
...
package com.bawei.animation;

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.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private TextView text;
    private Animator animator;
    private AnimatorSet animatorSet;
    private ValueAnimator valueAnimator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = findViewById(R.id.img);
        text = findViewById(R.id.txet);
        initAnimatorSet();
        initvalueAnimatar();
    }

    private void initAnimatorSet() {
        ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", 0f, 360f);
        ObjectAnimator translationX = ObjectAnimator.ofFloat(img, "translationX", 0f, 360f);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationY", 0f, 360f);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(img, "rotation", 0f, 360f);
        animatorSet = new AnimatorSet();
        animatorSet.play(rotation)
                .with(alpha)
                .before(translationY)
                .after(translationX);
        animatorSet.setDuration(5000);
        animatorSet.start();;
    }
    private void initvalueAnimatar() {
        valueAnimator = ValueAnimator.ofInt(100,500,100,500,600,800);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue;
                animatedValue = (int) animation.getAnimatedValue();
                text.layout(animatedValue,animatedValue,animatedValue+text.getWidth(),animatedValue+text.getHeight());
            }
        });
        valueAnimator.start();

    }
    private void initObjAnimatar(String state) {
        animator = ObjectAnimator.ofFloat(img,state,1f,5f,1f);
        animator.setDuration(5000);
        animator.start();

    }
}