美文网首页Android知识Android开发Android开发
项目实战:浅谈属性动画(1)-探索新玩法

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

作者: Mr_Quan | 来源:发表于2017-03-22 11:54 被阅读50次

属性动画是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

相关文章

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/jnconttx.html