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

属性动画是Google在3.0之后才提出的新动画框架,相比传统动画Animation只是系统不断调用onDraw方法重绘界面以实现动画效果。属性动画顾名思义是调用get、set方法真实改变属性。
传统Animation有很大的局限性:
1.只是重绘了动画,事件响应位置却没有改变,因此它不适用于具有交互动画的效果,只能做显示效果;
2.不断调用onDraw方法重绘很浪费资源;
3.位移,缩放,旋转,位移四种动画,组合可以实现丰富效果但仍不如属性。
因此,我们有必要好好学习属性动画。
看看Google的API Demos,有许多经典实用的小例子。(Google的心血)

是时候看一波代码了:

ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();  

让对象image_iv_main在X轴从0平移到200,时间持续1000ms,go!

如果这样会发生什么?

ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();  
ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F).setDuration(1000).start();  
ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F).setDuration(1000).start();  

对象会同时发生3个动画,X轴从0平移到200,Y轴从0平移到200,旋转360°,哈,你猜对了吗?
类似的效果还可以通过以下代码实现,有点代码复用的感觉。复用p1,p2,p3:

PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0F,360F);  
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,200F);  
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationX",0F,200F);  
ObjectAnimator.ofPropertyValuesHolder(image_iv_main,p1,p2,p3).setDuration(2000).start();  

进阶玩法,玩组合:

ObjectAnimator animator1 = ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F);  
ObjectAnimator animator2 = ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F);  
ObjectAnimator animator3 = ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F);  
AnimatorSet set = new AnimatorSet();  
set.playTogether(animator1,animator2,animator3);  
set.setDuration(2000);  
set.start();  

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

set.playSequentially(animator1,animator2,animator3);  

还可以调顺序:

set.play(animator1).with(animator2);  
set.play(animator3).after(animator2);  

更多玩法,等我们去探索~
最后送上完整源代码,high起来:

package com.example.quan.quanstudy.objectAnimator;  
  
import android.animation.AnimatorSet;  
import android.animation.ObjectAnimator;  
import android.view.View;  
import android.widget.ImageView;  
import android.widget.Toast;  
  
import com.example.quan.quanstudy.R;  
import com.example.quan.quanstudy.base.BaseActivity;  
  
/** 
 * Created by xingquan.he on 2017/3/15. 
 * Mr.Quan 
 * 属性动画第一课 
 * 项目实战:浅谈属性动画(1)-探索新玩法 http://blog.csdn.net/hxqneuq2012/article/details/52301791 
 */  
  
public class FirstClassActivity extends BaseActivity {  
  
    private ImageView mImageAnimator;  
  
    @Override  
    public int getLayoutId() {  
        return R.layout.activity_first_animator;  
    }  
  
    @Override  
    public void initView() {  
        mImageAnimator = (ImageView) findViewById(R.id.click_iv_animator);  
    }  
  
    public void click(View view){  
        Toast.makeText(this,"Clickd.",Toast.LENGTH_SHORT).show();  
    }  
  
    public void move(View view){  
//        ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();  
//        ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F).setDuration(1000).start();  
//        ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F).setDuration(1000).start();  
  
//        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0F,360F);  
//        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,200F);  
//        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationX",0F,200F);  
//        ObjectAnimator.ofPropertyValuesHolder(image_iv_main,p1,p2,p3).setDuration(2000).start();  
  
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageAnimator,"translationX",0F,200F);  
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageAnimator,"translationY",0F,200F);  
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageAnimator,"rotation",0F,360F);  
        AnimatorSet set = new AnimatorSet();  
        //set.playTogether(animator1,animator2,animator3);  
        //set.playSequentially(animator1,animator2,animator3);  
        set.play(animator1).with(animator2);  
        set.play(animator3).after(animator2);  
        set.setDuration(1000);  
        set.start();  
    }  
}  
<?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"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity">  
  
    <ImageView  
        android:layout_width="100dp"  
        android:layout_height="56dp"  
        android:src="@mipmap/quan_shijiazhuang"  
        android:onClick="click"  
        android:id="@+id/click_iv_animator"  
        />  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="move"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginBottom="50dp"  
        android:onClick="move"  
        android:id="@+id/move_btn_animator"  
        />  
  
</RelativeLayout>  

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

权兴权意
产品可以更优雅~
项目实战:浅谈属性动画(1)-探索新玩法 - hxqneuq2012的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/hxqneuq2012/article/details/62216824
项目源代码,欢迎提建议(star)。
https://github.com/HXQWill/QuanStudy/tree/master/app/src/main/java/com/example/quan/quanstudy/objectAnimator

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,455评论 25赞 708
  • 看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印象,因...
    DCbryant阅读 2,039评论 0赞 4
  • 这是我这个系列的目录,有兴趣的可以看下: android 动画系列 - 目录 啊,终于来到了属性动画这里了,属性动...
    前行的乌龟阅读 1,117评论 1赞 7
  • CYLTableViewPlaceHolder【一行代码完成“空TableView占位视图”管理】 导航 与其他框...
    iOS程序犭袁阅读 9,966评论 12赞 70
  • 今朝何见新人欢,他日杯盏共欢颜。京台仙舞漫楼阁,更声笑语花灯暖。人人送语声情淡,别语轻生笑意寒。亘古谁人诉情肠,哽...
    紫雨林阅读 317评论 0赞 1

友情链接更多精彩内容