Android属性动画赏析

为什么有Animation了,还要有Animator呢?
Animation有局限性,看下面代码.
XML布局:

<?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"
    tools:context="com.zhong.tutu.MainActivity">

   <android.support.v7.widget.AppCompatImageView
       android:id="@+id/image_view"
       android:onClick="click"
       android:background="@mipmap/ic_launcher"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:text="点击"
        android:onClick="move"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

MainActicity文件

package com.zhong.tutu;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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


    }
    public void click(View view){
        Toast.makeText(this,"我的土司",Toast.LENGTH_SHORT).show();
    }

    public void move(View view){
//        TranslateAnimation trans = new TranslateAnimation(0,200,0,100);
//        trans.setDuration(3000);
//        trans.setFillAfter(true);
        ImageView image_view =(ImageView) findViewById(R.id.image_view);
//        image_view.startAnimation(trans);
        ObjectAnimator.ofFloat(image_view,"translationX",0f,200f).setDuration(1000).start();
    }
}

可以发现Animation只是改变了位置,看似改变.实际上并没有改变,不适合交互;于是有了Animator;

package com.zhong.tutu;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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

    }

    public  void click(View view){
        Toast.makeText(this,"我是imageview",Toast.LENGTH_SHORT).show();
    }

    public  void move(View view){
       // TranslateAnimation transA = new TranslateAnimation(0,200,0,0);
//        transA.setDuration(1000);
//        transA.setFillAfter(true);

        ImageView imageView = (ImageView) findViewById(R.id.image_view);
//        imageView.startAnimation(transA);

        //属性动画
//        ObjectAnimator.ofFloat(imageView,"translationX",0f,200f).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"translationY",0f,200f).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"rotation",0f,200f).setDuration(400).start();

    /*    //同时作用于3个动画  PropertyValuesHolder对动画进行优化
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0f, 200f);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX", 0f, 200f);
        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY", 0f, 200f);
        ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();*/

        ObjectAnimator t1 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);
        ObjectAnimator t2 =ObjectAnimator.ofFloat(imageView,"translationY",0f,200f);
        ObjectAnimator t3 = ObjectAnimator.ofFloat(imageView,"rotation",0f,200f);

       AnimatorSet set = new AnimatorSet();
        set.play(t1).with(t2);
        set.play(t3).after(t2);
      //  set.playSequentially(t1,t2,t3);
        set.setDuration(1000);
        set.start();

    }
}

动画的监听事件

package com.zhong.animatordemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

}
public void click(View view){
    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"alpha",0f,1f);
    objectAnimator.setDuration(1000);
    objectAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            Toast.makeText(MainActivity.this," onAnimationEnd",Toast.LENGTH_SHORT).show();
        }
    });
   /* objectAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Toast.makeText(MainActivity.this," onAnimationEnd",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });*/
    objectAnimator.start();

}

}

“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值