天天看點

Android動畫--屬性動畫Property Animation

簡介

屬性動畫包含:
ObjectAnimator   動畫的執行類
ValueAnimator    動畫的執行類
AnimatorSet      用于控制一組動畫的執行:線性,一起,每個動畫的先後執行等。
AnimatorInflater 使用者加載屬性動畫的xml檔案
           

ObjectAnimator 單一屬性動畫

縮放X軸:ScaleX

ObjectAnimator().ofFloat(imageView,”ScaleY”,1.0f,0.0f).setDuration(3000).start();

縮放Y軸:ScaleY

ObjectAnimator().ofFloat(imageView,”ScaleX”,1.0f,0.0f).setDuration(3000).start();

旋轉動畫(X軸):rotationX

ObjectAnimator().ofFloat(imageView,”rotationX”,0.0f,360f).setDuration(3000).start();

旋轉動畫(Y軸):rotationY

ObjectAnimator().ofFloat(imageView,”rotationY”,0.0f,360f).setDuration(3000).start();

旋轉動畫(中心點):rotation

ObjectAnimator().ofFloat(imageView,”rotation”,0.0f,360f).setDuration(3000).start();

淡入淡出:alpha

ObjectAnimator().ofFloat(imageView,”alpha”,1.0f,0.0f).setDuration(3000).start();

位移動畫(X軸):

ObjectAnimator().ofFloat(imageView,”translationX”,0.0f,100.0f).setDuration(3000);

位移動畫(Y軸):

ObjectAnimator().ofFloat(imageView,”translationY”,0.0f,100.0f).setDuration(3000);

代碼實作如下

imageView.clearAnimation();
                ObjectAnimator objectAnimator=new ObjectAnimator().ofFloat(imageView,"translationX",,,,).setDuration();
                objectAnimator.start();
           

在xml中實作

  • 首先需要在res檔案夾下建立animator檔案夾,在其中定義xml檔案如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000">
    <objectAnimator
        android:propertyName="scaleX"
        android:repeatCount="-1"
        android:valueFrom="0.0f"
        android:valueTo="1.0f"></objectAnimator>
</set>
           
  • 相應的代碼中實作如下
Animator animatorObjext = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
                animatorObjext.setTarget(imageView2);
                animatorObjext.start();
           
Android動畫--屬性動畫Property Animation

ValueAnimator

  • 代碼實作如下
imageView2.clearAnimation();
                imageView2.setImageResource(R.mipmap.ic_launcher);
                //這裡的屬性值可以根據需要設定多個
                ValueAnimator animator = ValueAnimator.ofFloat(f, f, f, f, f);//設定屬性值
                animator.setTarget(imageView2);//設定操作對象
                animator.setDuration().start();//動畫開始
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        imageView2.setScaleY((Float) animation.getAnimatedValue());//設定Y軸上的變化
                        imageView2.setScaleX((Float) animation.getAnimatedValue());//設定X軸上的變化
                    }
                });
           
Android動畫--屬性動畫Property Animation

PropertyValuesHolder 組合動畫

  • 就是設定多個不同的動畫,同時讓他們加載就好了
  • 相應的代碼實作
imageView.clearAnimation();
                PropertyValuesHolder scaleX=PropertyValuesHolder.ofFloat("scaleX",,,);
                PropertyValuesHolder scaleY=PropertyValuesHolder.ofFloat("scaleY",,,);
                PropertyValuesHolder rotattion=PropertyValuesHolder.ofFloat("rotation",,);
                ObjectAnimator objectAnimator2=new ObjectAnimator().ofPropertyValuesHolder(imageView,scaleX,scaleY,rotattion).setDuration();
                objectAnimator2.start();
           
Android動畫--屬性動畫Property Animation

AnimatorSet 組合動畫(可以設定動畫的順序)

  • 相應的代碼如下
ObjectAnimator objectAnimator3=new ObjectAnimator().ofFloat(imageView,"translationX",f,f,f,f).setDuration();
                ObjectAnimator objectAnimator4=new ObjectAnimator().ofFloat(imageView,"scaleX",,,).setDuration();
                ObjectAnimator objectAnimator5=new ObjectAnimator().ofFloat(imageView,"rotation",,).setDuration();
                AnimatorSet animatorSet=new AnimatorSet();
                //設定動畫同時執行
                animatorSet.playTogether(objectAnimator3,objectAnimator4);
                //設定動畫的執行順序
                animatorSet.play(objectAnimator5).after(objectAnimator3);
                animatorSet.start();
           
Android動畫--屬性動畫Property Animation

]

AnimatorInflater 加載屬性動畫的xml檔案

  • xml還是在res檔案夾下建立的animator檔案夾下的xml檔案
  • 相應的xml檔案的定義如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together"
    android:duration="1000">
    <!-- ordering 指動畫是一起播放還是一個接一個播放-->
    <objectAnimator
        android:propertyName="scaleX"
        android:valueFrom="0.0"
        android:valueTo="1.0"></objectAnimator>
    <objectAnimator
        android:propertyName="scaleY"
        android:valueFrom="0.0"
        android:valueTo="1.0"></objectAnimator>
</set>
           
  • 相應的邏輯代碼如下
imageView2.clearAnimation();
                imageView2.setImageResource(R.mipmap.ic_launcher);
                Animator animatorMy = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animatorinflater);
                animatorMy.setTarget(imageView2);
                animatorMy.start();
           
Android動畫--屬性動畫Property Animation