Android動畫--屬性動畫Property Animation

來源:互聯網
上載者:User

標籤:

簡介
屬性動畫包含: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",0.0f,100.0f,0.0f,100.0f).setDuration(3000);                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();

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

PropertyValuesHolder 組合動畫
  • 就是設定多個不同的動畫,同時讓他們載入就好了
  • 相應的代碼實現
                imageView.clearAnimation();                PropertyValuesHolder scaleX=PropertyValuesHolder.ofFloat("scaleX",1,0,1);                PropertyValuesHolder scaleY=PropertyValuesHolder.ofFloat("scaleY",1,0,1);                PropertyValuesHolder rotattion=PropertyValuesHolder.ofFloat("rotation",0,360);                ObjectAnimator objectAnimator2=new ObjectAnimator().ofPropertyValuesHolder(imageView,scaleX,scaleY,rotattion).setDuration(3000);                objectAnimator2.start();

AnimatorSet 組合動畫(可以設定動畫的順序)
  • 相應的代碼如下
                ObjectAnimator objectAnimator3=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000);                ObjectAnimator objectAnimator4=new ObjectAnimator().ofFloat(imageView,"scaleX",1,0,1).setDuration(3000);                ObjectAnimator objectAnimator5=new ObjectAnimator().ofFloat(imageView,"rotation",0,360).setDuration(3000);                AnimatorSet animatorSet=new AnimatorSet();                //設定動畫同時執行                animatorSet.playTogether(objectAnimator3,objectAnimator4);                //設定動畫的執行順序                animatorSet.play(objectAnimator5).after(objectAnimator3);                animatorSet.start();

]

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.