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

安卓:利用ObjectAnimator实现动画效果

程序员文章站 2022-05-05 12:38:04
...

1、编辑布局文件

添加5个按钮和一个图像按钮ImageButton
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.animator.MainActivity"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="透明动画"
        />
    <Button
        android:id="@+id/btn_rotationY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋转动画"
        />
    <Button
        android:id="@+id/btn_scaleX"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="缩放动画"
        />
    <Button
        android:id="@+id/btn_translationY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="平移动画"
        /><Button
    android:id="@+id/btn_AnimatorSet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="集合动画"
    />
    <ImageButton
        android:id="@+id/img"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/aaa"
    android:layout_gravity="center"
    android:background="@null"/>
</LinearLayout>

2、编辑MainActivity

通过ObjectAnimator实现对图像的具体操作。
MainActivity.java

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{

    private Button btnAlpha;
    private Button btnRotationy;
    private Button btnScaleX;
    private Button btnTranslationY;
    private View btnAnimatorSet;
    private View img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
    }

    private void initView() {//找控件设置点击事件
        btnAlpha = (Button) findViewById(R.id.btn_alpha);
        btnRotationy = (Button) findViewById(R.id.btn_rotationY);
        btnScaleX = (Button) findViewById(R.id.btn_scaleX);
        btnTranslationY = (Button) findViewById(R.id.btn_translationY);
        btnAnimatorSet =findViewById(R.id.btn_AnimatorSet);
        img=findViewById(R.id.img);
        btnAlpha.setOnClickListener(this);
        btnRotationy.setOnClickListener(this);
        btnScaleX.setOnClickListener(this);
        btnTranslationY.setOnClickListener(this);
        btnAnimatorSet.setOnClickListener(this);
        img.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

             //注意:以下实现属性动画的 ObjectAnimator.ofFloat中第二个参数都是跟自己要实现的动画名称一样,否则会出错。例如透明动画第二个参数就是它的英文单词alpha,不能写错;

            case R.id.btn_alpha://透明动画
                ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
                alpha.setDuration(2000);
                alpha.setRepeatCount(1);
                alpha.setRepeatMode(ObjectAnimator.RESTART);
                alpha.start();
                break;
            case R.id.btn_rotationY://旋转动画
                ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", new float[]{90f, 180f, 270f, 360f});
                rotationY.setDuration(2000);
                rotationY.setRepeatCount(1);
                rotationY.setRepeatMode(ObjectAnimator.RESTART);
                rotationY.start();
                break;
            case R.id.btn_scaleX://缩放动画
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 1f});
                scaleX.setDuration(2000);
                scaleX.setRepeatCount(1);
                scaleX.setRepeatMode(ObjectAnimator.RESTART);
                scaleX.start();
                break;
            case R.id.btn_translationY://平移动画
                ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationY", new float[]{10f, 20f, 30f, 40f, 60f,80f});
                translationY.setDuration(2000);
                translationY.setRepeatCount(1);
                translationY.setRepeatMode(ObjectAnimator.RESTART);
                translationY.start();
                break;
            case R.id.btn_AnimatorSet://集合动画
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator oa = ObjectAnimator.ofFloat(img, "translationX", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
                oa.setDuration(3000);
                ObjectAnimator oa2 = ObjectAnimator.ofFloat(img, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
                oa2.setDuration(3000);
                animatorSet.playTogether(oa, oa2);
                animatorSet.start();
                break;
            case R.id.img://点击动画
                Toast.makeText(this, "你点击了我", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

3、运行并观察结果

(图片太大了就不放了,反正是可以运行出来的~~!)