Android中属性动画4----ValueAnimator拿到每一次变化的值

来源:互联网 发布:sql server 排序规则 编辑:程序博客网 时间:2024/06/06 06:35

代码:

Main4Activity
package com.zhh.android;import android.animation.Animator;import android.animation.AnimatorListenerAdapter;import android.animation.ValueAnimator;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.LinearInterpolator;import android.widget.Button;/** * ValueAnimator的使用,拿到每一次变化的值 */public class Main4Activity extends Activity {    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main4);        button = (Button) findViewById(R.id.button);//      按钮点击事件        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {//              设置0到100的整数变化                final ValueAnimator valueAnimator = ValueAnimator.ofInt(0,10);//              整个事件段是5秒                valueAnimator.setDuration(10000);//              数字均匀变化,也可设置其他的变化方式,先快后慢,先慢后快等......                valueAnimator.setInterpolator(new LinearInterpolator());//              监听每次改变时的值                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                    @Override                    public void onAnimationUpdate(ValueAnimator animation) {//                      拿到每一次变化的值                        Integer value = (Integer) animation.getAnimatedValue();//                      把只设置到按钮上                        button.setText(value+"");                    }                });                valueAnimator.start();            }        });    }}
activity_main4.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.zhh.android.Main4Activity"    android:orientation="vertical"    >    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮"        android:layout_centerInParent="true"        /></RelativeLayout>
参考视频:
http://www.imooc.com/learn/263
源码下载:
http://download.csdn.net/download/zhaihaohao1/10126123

阅读全文
0 0
原创粉丝点击