代码库/互动按钮/属性动画

Interpolators插值器:可以定义动画变换速率

LinearInterpolator:恒定变化的插值器

MainAc

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YRC on 2017/10/27.
 */

public class anim extends AppCompatActivity implements View.OnClickListener{
    private List<ImageView> imageViewList=new ArrayList<>();
    private int[]  mIv={R.id.imageView_a,R.id.imageView_b,R.id.imageView_c,R.id.imageView_d,R.id.imageView_e};
    private boolean mFlag=true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim);
        for (int i=0;i<5;i++){
            ImageView iv= (ImageView) findViewById(mIv[i]);
            iv.setOnClickListener(this);
            imageViewList.add(iv);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_a:
                if (mFlag){
                    startAnim();
                }else {
                    closeAnim();
                }
                break;
            default:
                closeAnim();
                    Toast.makeText(anim.this,""+v.getId(),Toast.LENGTH_SHORT).show();
                break;

        }
    }

    private void closeAnim() {
        ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(imageViewList.get(0),"Alpha",0.5F,1F);
        ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(imageViewList.get(1),"translationX",200F,0);
        ObjectAnimator objectAnimator3=ObjectAnimator.ofFloat(imageViewList.get(2),"translationY",200F,0);
        ObjectAnimator objectAnimator4=ObjectAnimator.ofFloat(imageViewList.get(3),"translationX",-200F,0);
        ObjectAnimator objectAnimator5=ObjectAnimator.ofFloat(imageViewList.get(4),"translationY",-200F,0);
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.setInterpolator(new LinearInterpolator());
        animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4,objectAnimator5);
        animatorSet.start();
        mFlag=true;
    }

    private void startAnim() {
        ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(imageViewList.get(0),"Alpha",1F,0.5F);
        ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(imageViewList.get(1),"translationX",200F);
        ObjectAnimator objectAnimator3=ObjectAnimator.ofFloat(imageViewList.get(2),"translationY",200F);
        ObjectAnimator objectAnimator4=ObjectAnimator.ofFloat(imageViewList.get(3),"translationX",-200F);
        ObjectAnimator objectAnimator5=ObjectAnimator.ofFloat(imageViewList.get(4),"translationY",-200F);
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(500);
        //LinearInterpolator:恒定变化的插值器
        animatorSet.setInterpolator(new LinearInterpolator());
        animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4,objectAnimator5);
        animatorSet.start();
        mFlag=false;
    }
}

xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_b"
        android:src="@drawable/b"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_c"
        android:src="@drawable/c"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_d"
        android:src="@drawable/d"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_e"
        android:src="@drawable/e"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_a"
        android:src="@drawable/a"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值