项目实战:浅谈属性动画(1)-探索新玩法

属性动画是Google在3.0之后才提出的新动画框架,相比传统动画Animation只是系统不断调用onDraw方法重绘界面以实现动画效果。属性动画顾名思义是调用get、set方法真实改变属性。

传统Animation有很大的局限性:

1.只是重绘了动画,事件响应位置却没有改变,因此它不适用于具有交互动画的效果,只能做显示效果;

2.不断调用onDraw方法重绘很浪费资源;

3.位移,缩放,旋转,位移四种动画,组合可以实现丰富效果但仍不如属性。

因此,我们有必要好好学习属性动画。

看看Google的API Demos,有许多经典实用的小例子。(Google的心血)


是时候看一波代码了:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();  
让对象image_iv_main在X轴从0平移到200,时间持续1000ms,go!


如果这样会发生什么?

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();  
  2. ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F).setDuration(1000).start();  
  3. ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F).setDuration(1000).start();  

对象会同时发生3个动画,X轴从0平移到200,Y轴从0平移到200,旋转360°,哈,你猜对了吗?

类似的效果还可以通过以下代码实现,有点代码复用的感觉。复用p1,p2,p3:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0F,360F);  
  2. PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,200F);  
  3. PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationX",0F,200F);  
  4. ObjectAnimator.ofPropertyValuesHolder(image_iv_main,p1,p2,p3).setDuration(2000).start();  


进阶玩法,玩组合:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ObjectAnimator animator1 = ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F);  
  2. ObjectAnimator animator2 = ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F);  
  3. ObjectAnimator animator3 = ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F);  
  4. AnimatorSet set = new AnimatorSet();  
  5. set.playTogether(animator1,animator2,animator3);  
  6. set.setDuration(2000);  
  7. set.start();  

可以一起玩,还可以顺序玩:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. set.playSequentially(animator1,animator2,animator3);  
还可以调顺序:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. set.play(animator1).with(animator2);  
  2. set.play(animator3).after(animator2);  


更多玩法,等我们去探索~


最后送上完整源代码,high起来:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.quan.quanstudy.objectAnimator;  
  2.   
  3. import android.animation.AnimatorSet;  
  4. import android.animation.ObjectAnimator;  
  5. import android.view.View;  
  6. import android.widget.ImageView;  
  7. import android.widget.Toast;  
  8.   
  9. import com.example.quan.quanstudy.R;  
  10. import com.example.quan.quanstudy.base.BaseActivity;  
  11.   
  12. /** 
  13.  * Created by xingquan.he on 2017/3/15. 
  14.  * Mr.Quan 
  15.  * 属性动画第一课 
  16.  * 项目实战:浅谈属性动画(1)-探索新玩法 http://blog.csdn.net/hxqneuq2012/article/details/52301791 
  17.  */  
  18.   
  19. public class FirstClassActivity extends BaseActivity {  
  20.   
  21.     private ImageView mImageAnimator;  
  22.   
  23.     @Override  
  24.     public int getLayoutId() {  
  25.         return R.layout.activity_first_animator;  
  26.     }  
  27.   
  28.     @Override  
  29.     public void initView() {  
  30.         mImageAnimator = (ImageView) findViewById(R.id.click_iv_animator);  
  31.     }  
  32.   
  33.     public void click(View view){  
  34.         Toast.makeText(this,"Clickd.",Toast.LENGTH_SHORT).show();  
  35.     }  
  36.   
  37.     public void move(View view){  
  38. //        ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();  
  39. //        ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F).setDuration(1000).start();  
  40. //        ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F).setDuration(1000).start();  
  41.   
  42. //        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0F,360F);  
  43. //        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,200F);  
  44. //        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationX",0F,200F);  
  45. //        ObjectAnimator.ofPropertyValuesHolder(image_iv_main,p1,p2,p3).setDuration(2000).start();  
  46.   
  47.         ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageAnimator,"translationX",0F,200F);  
  48.         ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageAnimator,"translationY",0F,200F);  
  49.         ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageAnimator,"rotation",0F,360F);  
  50.         AnimatorSet set = new AnimatorSet();  
  51.         //set.playTogether(animator1,animator2,animator3);  
  52.         //set.playSequentially(animator1,animator2,animator3);  
  53.         set.play(animator1).with(animator2);  
  54.         set.play(animator3).after(animator2);  
  55.         set.setDuration(1000);  
  56.         set.start();  
  57.     }  
  58. }  

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".MainActivity">  
  12.   
  13.     <ImageView  
  14.         android:layout_width="100dp"  
  15.         android:layout_height="56dp"  
  16.         android:src="@mipmap/quan_shijiazhuang"  
  17.         android:onClick="click"  
  18.         android:id="@+id/click_iv_animator"  
  19.         />  
  20.   
  21.     <Button  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="move"  
  25.         android:layout_alignParentBottom="true"  
  26.         android:layout_centerHorizontal="true"  
  27.         android:layout_marginBottom="50dp"  
  28.         android:onClick="move"  
  29.         android:id="@+id/move_btn_animator"  
  30.         />  
  31.   
  32. </RelativeLayout>  

原创不易,转载请注明出处哈。

权兴权意

代码可以更优雅~

http://blog.csdn.net/hxqneuq2012/article/details/62216824


项目源代码,欢迎提建议(star)。

https://github.com/HXQWill/QuanStudy/blob/master/app/src/main/java/com/example/quan/quanstudy/objectAnimator/FirstClassActivity.java



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值