Android 属性动画的原理分析

前言:

  我们知道Android有PropertyAnimation和Tweened 动画,在前面,我简单的说Android Tweened的原理(Tweened animations 动画原理简单分析)。

       在此,我在描述下,在调用了view.startAnimation(animation)后,View 就开始不停的刷新和检查是否有动画属性的存在,如果有,就调用 继承Animation,中的protected void applyTransformation(float interpolatedTime, Transformation t) 方法中设置的view的一些效果的属性,如形状的变化都是通过Transformation t中的Matrix进行对view的变化(ps:这是一个随时间变化而,变化的martix值,通过这个值的设定达到对View变化的设置,跟随时间一起,就看起来像动画啦)。

    在API 13版本之后添加了一个属性动画。其一些介绍请看  PropertyAnimation(属性动画)

     

     其原理的分析,在下面的博客分析的比较详细。大家可以去看看这个大牛的这个专题。

Android动画系列:

android动画简介

Android动画进阶—使用开源动画库nineoldandroids

Android属性动画深入分析:让你成为动画牛人

Android源码分析—属性动画的工作原理

   通过对上面提到的文章的阅读,我对这个PropertyAnimation的原理也有了初步的了解了。

例子分析


              

点击了上面的Button 开始自动的将button的宽度变大。

第一是采用

Code

public class MainActivity extends Activity {

	Button button;
	Button button2;

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

		button = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				start();
			}
		});
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				start2();
			}
		});
	}
	public String TAG="start2";
	protected void start2() {
		
		System.out.println(">>>"+button2.getWidth());
		 ValueAnimator valueAnimator = ValueAnimator.ofInt(button2.getWidth(), 500);  
		 valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animator) {
				// TODO Auto-generated method stub
	            int currentValue = (Integer)animator.getAnimatedValue();  
	            // 通过 ValueAnimator自己的计算出相应的值,在对button的宽度进行设置,达到按钮不断的变宽的效果
	            button2.getLayoutParams().width = currentValue;  
	            button2.requestLayout();  
			}
		});
		  valueAnimator.setDuration(5000).start();  
	}
	protected void start() {
	// 其自动调用了button中的  with 这个属性随着时间的变化,对这个值进行赋值达到界面不停的 变化的过程,
//		通过宽度的不停的变大,达到 按钮不停的放大的效果
		ObjectAnimator.ofInt(button, "width", 500).setDuration(5000).start();
	}
}

布局文件

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

(ps:对其二者原理性的分析,详见上面推荐的链接。)

start2() 方法自己需要手动的对界面的属性(宽度)进行设置,随时间的变化,而宽度不停的扩大,达到界面中button宽度不断的放大的效果。

start() 方法在直接通过反射的方式对,获取到button的宽度,在其内部通过设置的值对button的值不断的放大。


为什么叫做propertyanimation ?

对需要设置的 Object target,也就是通过反射调用其属性值,通过在通过ObjectAnimator 继承的ValueAnimator 产生一个随时间变化的 值,设置给这个属性达到 target 不断的变化,达到的动画的效果。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值