ObjectAnimator详解(测试用,承接Android动画操作中的测试)

废话不多说直接看代码

需要注意的是ObjectAnimator.ofFloat(xiaoming, “age”, 0f,100f)传入的是float的值,所以在测试中age必须用float类型的,还必须有setAge的方法,否则无法完成值的传递

先看布局文件

<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"
    android:orientation="vertical"
    android:gravity="center"
   >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <TextView 
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50sp"/>
LinearLayout>

主程序代码

package com.test.testone;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.annotation.TargetApi;
import android.app.Activity;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
    private static final String TAG="AA";
    private Student xiaoming;
    private Button mButton;
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         mButton=(Button) findViewById(R.id.button);
         mTextView=(TextView) findViewById(R.id.text_view);
         mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 xiaoming=new Student();
                ObjectAnimator anim=  ObjectAnimator.ofFloat(xiaoming, "age", 0f,100f).setDuration(1000);
                  anim.addListener(new AnimatorListener()  
                    {

                        @Override
                        public void onAnimationStart(Animator animation) {
                             Log.d(TAG, "onAnimationStart");  

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                             Log.d("lalala", "onAnimationEnd");  
                             Log.d("lalala", xiaoming.getAge()+"");
                             mTextView.setText( xiaoming.getAge()+"");

                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                              Log.d(TAG, "onAnimationCancel");  

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {
                             Log.d(TAG, "onAnimationRepeat");  

                        }  

                    });  
                    anim.start();  

            }
        });

    }


}

辅助student类

package com.test.testone;

public class Student {
    private float age;

    public float getAge() {
        return age;
    }

    public void setAge(float age) {
        this.age = age;
    }

}

效果图

通过下图可以看到age的数值由默认值变为了传入值100,这就说明了ObjectAnimator的多用途,不仅可以用到动画效果中,还可以有更多的使用场合。
ObjectAnimator详解(测试用,承接Android动画操作中的测试)_第1张图片

你可能感兴趣的:(日记,Android技术开发)