属性动画ValueAnimator和ObjectAnimator的使用

首先很感谢郭神的文章,郭神可能也是因为篇幅的原因,有的基础东西一带而过,我在此写一些自己的理解。希望大家指教。这篇的主题是属性动画。

ValueAnimator
这个类是对值得平滑过渡的动画,什么意思呢。就是对数值在一定时间内进行平滑过渡。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 100f);
        animator.setDuration(5 * 1000);//设置动画的持续时间
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.i("hhhd", "value is"+animation.getAnimatedValue());
            }
        });
        animator.start();

    }
}

这里注册了一个监听器的回调,每次动画的状态发生改变,都会回调这个方法。我们这里打印出了animation.getAnimatedValue的值,实际上就是fraction的值,也就是完成度,一个动画从开始到结束,完成的百分比。当然fraction值为0-1。一部分日志打印为

这里写图片描述

ObjectAnimator
与ValueAnimator不同的是,ObjectAnimator是对 对象的属性 进行平滑过渡。

package com.example.administrator.myanimator;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by hd on 2015/12/19.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button alpha;
    private Button rotation;
    private Button translation;
    private Button scale;

    private TextView textView;

    ObjectAnimator animator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alpha = (Button) findViewById(R.id.alphaBtn);
        rotation = (Button) findViewById(R.id.rotationBtn);
        translation = (Button) findViewById(R.id.translationBtn);
        scale = (Button) findViewById(R.id.scale);

        alpha.setOnClickListener(this);
        rotation.setOnClickListener(this);
        translation.setOnClickListener(this);
        scale.setOnClickListener(this);

        textView = (TextView) findViewById(R.id.tvTest);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.alphaBtn:
            //透明度动画,值范围为0-1,0表示完全透明,1表示完全不透明
                animator = ObjectAnimator.ofFloat(textView, "alpha", 1, 0, 1);
                animator.setDuration(5 * 1000);
                break;
            case R.id.rotationBtn:
            //旋转动画,第一个数为初始状态,值可正可负
                animator = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
                animator.setDuration(5 * 1000);
                break;
            case R.id.translationBtn:
            //获取当前对象在屏幕中的X坐标
                float curTranslationx = textView.getTranslationX();
                //X轴方向平移动画,500f表示在curTranslation位置向右平移半屏,因为上下距离都默认为1000,-500f表示移动到curTranlation位置的左半屏幕位置,最后移回原位。
                animator = ObjectAnimator.ofFloat(textView, "translationX", curTranslationx, 500f, -500f,curTranslationx);
                animator.setDuration(5 * 1000);
                break;
            case R.id.scale:
            //比例动画,这里把对象的比例扩大或者缩小的动画
                animator = ObjectAnimator.ofFloat(textView, "scaleY", 1f, 5f, 4f, 3f, 1f);
                animator.setDuration(5 * 1000);
                break;
        }
            animator.start();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentTop="true">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/alphaBtn"
            android:text="alpha"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/rotationBtn"
            android:text="totation"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/translationBtn"
            android:text="translation"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/scale"
            android:text="scale"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tvTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!"
        android:textSize="30dp" />

</RelativeLayout>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值