android属性动画的研究

3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三种动画模式在SDK中被称为property animation,view animation,drawable animation。 可通过NineOldAndroids项目在3.0之前的系统中使用Property Animation


Property Animation故名思议就是通过动画的方式改变对象的属性了,我们首先需要了解几个属性:

Duration动画的持续时间,默认300ms。

Time interpolation:时间差值,乍一看不知道是什么,但是我说LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的了,定义动画的变化率。

Repeat count and behavior:重复次数、以及重复模式;可以定义重复多少次;重复时从头开始,还是反向。

Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。

Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。

相关的类

ObjectAnimator  动画的执行类,后面详细介绍

ValueAnimator 动画的执行类,后面详细介绍 

AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。

AnimatorInflater 用户加载属性动画的xml文件

TypeEvaluator  类型估值,主要用于设置动画操作属性的值。

TimeInterpolator 时间插值,上面已经介绍。

总的来说,属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。


TranslateAnimation animation=new TranslateAnimation(1, 12, 2, 23);

源码地址:http://download.csdn.net/detail/qqq01002013019/8724519



<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=".MainActivity" >


    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:onClick="vertical"
            android:text="垂直" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="paowuxian"
            android:text="抛物线运动" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:onClick="fadeout"
            android:text="淡出并删除" />
    </LinearLayout>


    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll"
        android:layout_marginLeft="100dp"
        android:clickable="true"
        android:onClick="animation"
        android:src="@drawable/ic_launcher" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="together"
            android:text="简单的多动画Together" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="playwithafter"
            android:text="多动画按次序执行" />
    </LinearLayout>


</RelativeLayout>



package com.examle.anddonghua;


import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;


/*
 * //tween动画的实现方式
 * 
 *  AnimationSet animationSet=new AnimationSet(true);
 * TranslateAnimation animation=new TranslateAnimation(1, 12, 2, 23);
 * view.startAnimation(animationSet);
 * 
 * AlphaAnimation ScaleAnimation RotateAnimation
 * 
 * Animation animation2=AnimationUtils.loadAnimation(MainActivity.this,
 * R.anim.ss); view.startAnimation(animation2);
 */


public class MainActivity extends Activity {
private ImageView imageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public void animation(final View view) {
// animation1(view);
animation2(view);


}


/**
* 实现一个动画更改多个效果

* @param view
*/
private void animation1(final View view) {
/**
* 提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、
* 作用的属性、动画开始、结束、以及中间的任意个属性值。

* 第一个参数为对象名,第二个为属性名,后面的参数为可变参数,如果values…参数只设置了一个值的话,那么会假定为目的值,
* 属性值的变化范围为当前值到目的值,为了获得当前值,该对象要有相应属性的getter方法:get<PropertyName>
*/
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "rotationX", 1.0f, 0.0f)
.setDuration(1000);
anim.start();


anim.addUpdateListener(new AnimatorUpdateListener() {


@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 按照时间插值和持续时间计算的cVal
float cVal = (Float) animation.getAnimatedValue();
view.setAlpha(cVal);
view.setScaleX(cVal);
view.setScaleY(cVal);
}
});
}


/**
* 实现一个动画更改多个效果(简单)

* @param view
*/


private void animation2(final View view) {


PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
0, 1f);
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
0, 1f);
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ)
.setDuration(1000).start();


}


// ----------------------------------------------------------------------------------------------------------------
/**
* image 垂直自由落体运动 ------------》 ValueAnimator实现动画

* @param view
*/


public void vertical(View view) {
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int screenWidth = window.getDefaultDisplay().getHeight();
// ValueAnimator并没有在属性上做操作
ValueAnimator animator = ValueAnimator.ofFloat(0, screenWidth
- imageView.getHeight());
animator.setTarget(imageView);
animator.setDuration(1000).start();


animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView.setTranslationY((Float) animation.getAnimatedValue());
}
});
}


/**
* image 抛物线运动 -------------------》ValueAnimator实现动画

* @param view
*/
public void paowuxian(View view) {
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0, 0));
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
// fraction = t / duration
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
Log.e("d", fraction * 3 + "");
// x方向200px/s ,则y方向0.5 * 10 * t
PointF point = new PointF();
point.x = 200 * fraction * 3;
point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
return point;
}
});


valueAnimator.start();
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF point = (PointF) animation.getAnimatedValue();
imageView.setX(point.x);
imageView.setY(point.y);


}
});
}


// --------------------------------------------------------------------------------------
/**
* 监听动画事件

* @param view
*/
public void fadeout(View view) {


ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f);
anim.setDuration(1000);
anim.addListener(new AnimatorListener() {


@Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub


}


@Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub


}


@Override
public void onAnimationEnd(Animator arg0) {
// 动画结束
ViewGroup parent = (ViewGroup) imageView.getParent();
if (parent != null) {
parent.removeView(imageView);
}
}


@Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub


}
});


// 也可以这样写
/*
* anim.addListener(new AnimatorListenerAdapter() {

* @Override public void onAnimationEnd(Animator animation) { // 动画结束
* ViewGroup parent = (ViewGroup) imageView.getParent(); if (parent !=
* null) { parent.removeView(imageView); } } });
*/
anim.start();
}


// ----------------------------------------AnimatorSet的使用--------------------


public void together(View view) {
AnimatorSet animationSet = new AnimatorSet();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleX",
1.0f, 2f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "scaleY",
1.0f, 2f);


animationSet.setDuration(2000);
animationSet.setInterpolator(new LinearInterpolator());
// 两个动画同时执行
animationSet.playTogether(animator1, animator2);
animationSet.start();


}


public void playwithafter(View view) {

float cx = imageView.getX();


//放大
ObjectAnimator anim1 = ObjectAnimator.ofFloat(imageView, "scaleX",
1.0f, 2f);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(imageView, "scaleY",
1.0f, 2f);
ObjectAnimator anim3 = ObjectAnimator.ofFloat(imageView, "x", cx, 0f);  //左移
ObjectAnimator anim4 = ObjectAnimator.ofFloat(imageView, "x", cx);      //右移到最终位置


/**
* anim1,anim2,anim3同时执行 anim4接着执行
*/
AnimatorSet animSet = new AnimatorSet();
animSet.play(anim1).with(anim2);
animSet.play(anim2).with(anim3);
animSet.play(anim4).after(anim3);
animSet.setDuration(1000);
animSet.start();
}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值