«

ObjectAnimator Demo

时间:2024-3-2 19:50     作者:韩俊     分类: Android


这里提供一个 ObjectAnimator 以供参考:这个Demo基本实现了从左向右(从左边出来,然后停留在中间放大,然后从右边缩小消失的效果)

代码比较简单,就一个类FiveActivity.java即可

package studio.neulion.com.tasktest;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.BounceInterpolator;

public class FiveActivity extends Activity
{
    private View mTestView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initView();

        translateX(); //仅仅一个动画

        translateXScaleBoth(); //实现叠加动画

        translateXScaleOrder(); //实现顺序动画
    }

    private void initView()
    {
        mTestView = findViewById(R.id.test_view);
    }

    @TargetApi(VERSION_CODES.HONEYCOMB)
    private void translateX()
    {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTestView, "translationX", -(dm.widthPixels / 2 + mTestView.getWidth() / 2), 0F);

        objectAnimator.setDuration(1000);
        objectAnimator.setInterpolator(new BounceInterpolator());
        objectAnimator.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationStart(Animator animation)
            {
                super.onAnimationStart(animation);
                mTestView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation)
            {
                super.onAnimationEnd(animation);
            }
        });

        objectAnimator.start();
    }

    @TargetApi(VERSION_CODES.HONEYCOMB)
    private void translateXScaleBoth()
    {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mTestView, pvhX, pvhY, pvhZ);
        objectAnimator.setDuration(1000);
        objectAnimator.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationStart(Animator animation)
            {
                super.onAnimationStart(animation);
                mTestView.setVisibility(View.VISIBLE);
            }
        });

        objectAnimator.start();
    }

    @TargetApi(VERSION_CODES.HONEYCOMB)
    private void translateXScaleOrder()
    {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        ObjectAnimator translationXin = ObjectAnimator.ofFloat(mTestView, "translationX", -(dm.widthPixels / 2 + mTestView.getWidth() / 2), 0F);
        ObjectAnimator scaleXin = ObjectAnimator.ofFloat(mTestView, "scaleX", 1f, 0f, 6f);
        ObjectAnimator scaleYin = ObjectAnimator.ofFloat(mTestView, "scaleY", 1f, 0f, 6f);
        ObjectAnimator translationXout = ObjectAnimator.ofFloat(mTestView, "translationX", 0F, (dm.widthPixels / 2 + mTestView.getWidth() / 2));

        ObjectAnimator scaleXout = ObjectAnimator.ofFloat(mTestView, "scaleX", 6f, 0f);
        ObjectAnimator scaleYout = ObjectAnimator.ofFloat(mTestView, "scaleY", 6f, 0f);

        AnimatorSet set = new AnimatorSet();
        set.play(translationXin).before(scaleXin);
        set.play(scaleXin).with(scaleYin);
        set.play(translationXout).with(scaleXout).with(scaleYout).after(scaleXin);

        set.setDuration(1000);
        set.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationStart(Animator animation)
            {
                super.onAnimationStart(animation);
                mTestView.setVisibility(View.VISIBLE);
            }
        });
        set.start();
    }
}
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐