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

属性动画

程序员文章站 2022-05-04 07:51:28
...
package com.example.animatordemo;

import android.animation.ObjectAnimator;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView image_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_translate).setOnClickListener(this);
        image_view = findViewById(R.id.image_view);
        findViewById(R.id.btn_alpha).setOnClickListener(this);
        findViewById(R.id.btn_argb).setOnClickListener(this);
        findViewById(R.id.btn_rotation).setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_translate:
                translation();
                break;
            case R.id.btn_argb:
                argb();
                break;
            case R.id.btn_rotation:
                rotation();
                break;
            case R.id.btn_alpha:
                alpha();
                break;
        }

    }

    private void rotation() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image_view,"rotationY",0,360);
        objectAnimator.setDuration(3000);
        objectAnimator.setRepeatCount(-1);
        objectAnimator.start();
    }

    private void alpha() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image_view,"alpha",1,0.8f,0.5f,0,0.5f,1);
        objectAnimator.setDuration(3000);
        objectAnimator.setRepeatCount(-1);
        objectAnimator.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void argb() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofArgb(image_view,"backgroundColor",0xffff00ff, 0xffffff00, 0xffff00ff);
        objectAnimator.setDuration(3000);
        objectAnimator.setRepeatCount(-1);
        objectAnimator.start();
    }

    private void translation() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image_view,"translationX",0,800,0);
        objectAnimator.setDuration(5000);
        objectAnimator.setRepeatCount(-1);
        objectAnimator.start();
    }
}